我想通過在c#應用程序中執行的sql代碼更新表。爲此,我使用生成的MSSMS生成的數據並手動將其保存爲一個sql文件。然後c#讀取文件並嘗試執行它,但它不能。如果我單獨使用sql代碼,它會起作用,但不能在用c#函數讀取時使用。我的C#代碼有什麼問題?通過C#代碼更新SQL表
的SQL代碼生成我MSSMS:
/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.tTest ADD
NewColumn int NULL
GO
ALTER TABLE dbo.tTest SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
C#代碼讀取它:
string content = string.Empty;
try
{
content = File.ReadAllText(string.Format(@"C:\temp\{0}.sql", name));
SqlConnection conn = new SqlConnection(ConnectionString);
SqlCommand command = new SqlCommand(content, conn);
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
附帶從c#函數(控制檯消息)的輸出:
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
你的先生,是一個天才要比! – Dave 2014-09-22 07:27:02