سلام دوست عزیز
ممنون از راهنماییتون
اما مهمترین قسمت سئوال من رو جواب ندادید!
ببخشید که من سئوالات ابتدایی می پرسم!
من متوجه نشدم که این دستورات را باید در کجا تایپ کنم و چگونه اجرا کنم؟
Printable View
سلام دوست عزیز
ممنون از راهنماییتون
اما مهمترین قسمت سئوال من رو جواب ندادید!
ببخشید که من سئوالات ابتدایی می پرسم!
من متوجه نشدم که این دستورات را باید در کجا تایپ کنم و چگونه اجرا کنم؟
سلام
نقل قول:
من متوجه نشدم که این دستورات را باید در کجا تایپ کنم و چگونه اجرا کنم؟
در دات نتی ترین حالت و بدون هیچ ابزاری کافیست یک System.Data.SqlClient.SqlCommand بسازید و دستورات فوق را به آن بدهیدنقل قول:
دستور زیر را میتوانید از طریق یک شی Command و یا گزینه NewQuery در VS و SSMS اجرا کنید
یک برنامه ساده بسازید با دو TextBox ، یکی برای ConnecTionString و یک بزرگتر و چند خطی برای SQL با یک دکمه اجرا...
کدش هم خیلی خیلی ساده میشود
کد://C#.Net
using (System.Data.SqlClient.SqlConnection dbCon = new System.Data.SqlClient.SqlConnection(this.TextBox1.Text)){
using (System.Data.SqlClient.SqlCommand dbCom = new System.Data.SqlClient.SqlCommand(this.TextBox2.Text, dbCon)){
dbCon.Open();
dbCom.ExecuteNonQuery();
}}
'VB.Net
Using _
dbCon As New System.Data.SqlClient.SqlConnection(Me.TextBox1.Text), _
dbCom As New System.Data.SqlClient.SqlCommand(Me.TextBox2.Text, dbCon)
dbCon.Open()
dbCom.ExecuteNonQuery()
End Using
فکر کنم شما در مورد قسمت دوم بحث صحبت می کنید اما منظور من قسمت اول بود!
سلام
عزیز دل بابا ! قسمت اول و دوم نداره!
دستور SQL دستور SQL است! (بازم چشم بسته زیر آبی رفتم)
میخواهید یک SELECT یا UPDATE ناقابل باشد یا یک DBCC یا یک مجموعه عظیم چند کیلوبایتی، هیچ فرقی ندارد.
فکر کردید برای چی گفتم که از یک TextBox بزرگ چند خطی استفاده کنید؟؟؟؟؟
برای اینکه فقط یک خط ... DBCC CHECKIDENT را در ان تایپ کنید؟
T-SQL خودش برای خودش یک نیمچه زبان برنامه نویسی شده، یک SELECT و INSERT نیست که در یک ساعت یاد میدهند!
تست کنید برادر، جواب نداد،همچنان در خدمت هستم.
سلام
من دیتابیس را به SqlServer و Attach کردم و
و دستور نوشته شده:کد:ConnectionString:
Data Source=.;Initial Catalog=markazdb;Integrated Security=False;Persist Security Info=True;User ID=sa;Password=1
و خطای داده شده:کد:BEGIN TRANSACTION;
USE [MarkazDB123];
CREATE TABLE tmp_Customer
(
CuId bigint NOT NULL IDENTITY (1, 1),
Active bit NULL,
Date nvarchar(10) NULL,
Time nvarchar(5) NULL,
FName nvarchar(10) NULL,
LName nvarchar(20) NULL,
Gen nvarchar(1) NULL,
Position nvarchar(1) NULL,
Type nvarchar(1) NULL,
Cover nvarchar(1) NULL,
Rank nvarchar(1) NULL,
Problem nvarchar(1) NULL,
Age nvarchar(2) NULL,
Tel nvarchar(20) NULL,
Addr nvarchar(100) NULL,
OPosition nvarchar(15) NULL,
OType nvarchar(15) NULL,
OCover nvarchar(15) NULL,
ORank nvarchar(15) NULL,
OProblem nvarchar(15) NULL
) ON [PRIMARY];
SET IDENTITY_INSERT tmp_Customer ON;
INSERT INTO tmp_Customer
([CuId], [Date], [Time], [Fname], [Lname], [Gen], [Position], [Type],
[Cover], [Rank], [Problem], [Age], [Tel], [Addr], [Oposition],
[Otype], [Ocover], [Orank], [Oproblem])
SELECT
[CuId], [Date], [Time], [Fname], [Lname], [Gen], [Position], [Type],
[Cover], [Rank], [Problem], [Age], [Tel], [Addr], [Oposition],
[Otype], [Ocover], [Orank], [Oproblem]
FROM [Customer] WITH (HOLDLOCK TABLOCKX);
SET IDENTITY_INSERT tmp_Customer OFF;
DROP TABLE [Customer];
EXECUTE sp_rename N'tmp_Customer', N'Customer', 'OBJECT' ;
ALTER TABLE [Customer]
ADD CONSTRAINT
PK_ Customer PRIMARY KEY CLUSTERED
(CuId)
WITH
(STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY];
COMMIT TRANSACTION;
و متن کامل خطا:کد:Incorrect syntax near 'Customer'.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
کد:System.Data.SqlClient.SqlException was unhandled
Message="Incorrect syntax near 'Customer'.\r\nIncorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon."
Source=".Net SqlClient Data Provider"
ErrorCode=-2146232060
Class=15
LineNumber=59
Number=102
Procedure=""
Server="."
State=1
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at WindowsApplication1.Form1.button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Morteza\Desktop\WindowsApplication1\WindowsApplication1\Form1.cs:line 25
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsApplication1.Program.Main() in C:\Documents and Settings\Morteza\Desktop\WindowsApplication1\WindowsApplication1\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
سلام
من دیتابیس را به SqlServer و Attach کردم و
و دستور نوشته شده:کد:ConnectionString:
Data Source=.;Initial Catalog=markazdb;Integrated Security=False;Persist Security Info=True;User ID=sa;Password=1
و خطای داده شده:کد:BEGIN TRANSACTION;
USE [MarkazDB];
CREATE TABLE tmp_Customer
(
CuId bigint NOT NULL IDENTITY (1, 1),
Active bit NULL,
Date nvarchar(10) NULL,
Time nvarchar(5) NULL,
FName nvarchar(10) NULL,
LName nvarchar(20) NULL,
Gen nvarchar(1) NULL,
Position nvarchar(1) NULL,
Type nvarchar(1) NULL,
Cover nvarchar(1) NULL,
Rank nvarchar(1) NULL,
Problem nvarchar(1) NULL,
Age nvarchar(2) NULL,
Tel nvarchar(20) NULL,
Addr nvarchar(100) NULL,
OPosition nvarchar(15) NULL,
OType nvarchar(15) NULL,
OCover nvarchar(15) NULL,
ORank nvarchar(15) NULL,
OProblem nvarchar(15) NULL
) ON [PRIMARY];
SET IDENTITY_INSERT tmp_Customer ON;
INSERT INTO tmp_Customer
([CuId], [Date], [Time], [Fname], [Lname], [Gen], [Position], [Type],
[Cover], [Rank], [Problem], [Age], [Tel], [Addr], [Oposition],
[Otype], [Ocover], [Orank], [Oproblem])
SELECT
[CuId], [Date], [Time], [Fname], [Lname], [Gen], [Position], [Type],
[Cover], [Rank], [Problem], [Age], [Tel], [Addr], [Oposition],
[Otype], [Ocover], [Orank], [Oproblem]
FROM [Customer] WITH (HOLDLOCK TABLOCKX);
SET IDENTITY_INSERT tmp_Customer OFF;
DROP TABLE [Customer];
EXECUTE sp_rename N'tmp_Customer', N'Customer', 'OBJECT' ;
ALTER TABLE [Customer]
ADD CONSTRAINT
PK_ Customer PRIMARY KEY CLUSTERED
(CuId)
WITH
(STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY];
COMMIT TRANSACTION;
و متن کامل خطا:کد:Incorrect syntax near 'Customer'.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
کد:System.Data.SqlClient.SqlException was unhandled
Message="Incorrect syntax near 'Customer'.\r\nIncorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon."
Source=".Net SqlClient Data Provider"
ErrorCode=-2146232060
Class=15
LineNumber=59
Number=102
Procedure=""
Server="."
State=1
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at WindowsApplication1.Form1.button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Morteza\Desktop\WindowsApplication1\WindowsApplication1\Form1.cs:line 25
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsApplication1.Program.Main() in C:\Documents and Settings\Morteza\Desktop\WindowsApplication1\WindowsApplication1\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
سلام
به نظر می رسد انشا ا... از sqlserver2005 استفاده میکنید؟
بلوک (WITH (HOLDLOCK TABLOCKX را حذف کنید و مجدداً تست کنید.
سلام
فکر می کنم اشتباه تایپی داشتم، چون روی یک جدول دیگر جواب داد(همون کد اولی)
ممنون
سلام
ببخشید من اینقدر مزاحم شما می شم، یک سئوال داشتم
من یک جدول دارم که اطلاعات آن از قبل مشخص می باشد (اطلاعات این جدول در هنگام Design برنامه توسط برنامه نویس وارد شده است) این جدول را چگونه به دیتابیس کاربر اضافه کنم؟
سلام
اتفقاً کار خوبی است، معمولاً برای نظیر کردن اعداد ایندکس شده خاص در سطح فیلدهای جداول مختلف دیتابیس که اعداشان یک معنی خاص دارد، بد نیست یک جدول ثابت اضافه کنیم.نقل قول:
من یک جدول دارم که اطلاعات آن از قبل مشخص می باشد (اطلاعات این جدول در هنگام Design برنامه توسط برنامه نویس وارد شده است) این جدول را چگونه به دیتابیس کاربر اضافه کنم؟
ولی خوب این جدول مگر با جداول دیگر تفاوتی دارد؟ مثل بقیه بسازیدش!
اگر هم منظورتان آن است که پروژه را تحویل داده اید و حالا طبق آپدید جدید، باید جدولی اضافه شود، این هم مشکلی ندارد.
کافیست از همان شبه اسکریپ بالا و بخش های CREATE TABLE و ALTER TABLE استفاده کنید، اگر خواستید حتی میتوانید مقادیر جدول را هم با یک سری دستور INSERT در همان اسکریپ اضافه کنید.
البته بعد از ساخت جدول، برای INSERT کردن اطلاعات میتوانید از نسخه ذخیره شده اطلاعات در xml و لود در dataset و اعمال در دیتابیس استفاده کنید.
همانطور که قبلاً هم اشاره ای کردم، شما به راحیت میتوانید با خود دات نت یک پروزه کوچک exe بسازید که مسئول آپگراید برنامه تان باشد، به طوری که با اجرای آن در رایانه هدف، این exe کوچک خودش تمام فایلهای جدید را کپی و یا replace کند و تغییرات لازم را هم در دیتابیس اعمال کند.
در هر صورت همچنان سوالی داشتید بپرسید.