2012-11-09 68 views
0

當我嘗試運行這個時,我得到一個SqlException消息「關鍵字'PROCEDURE'附近的語法不正確。錯誤的SQL語法錯誤ADO.NET

120: cmd.CommandText = @"CREATE PROCEDURE FindState @Name nvarchar(50) 
    AS SELECT IDState FROM State WHERE [email protected]"; 
121: cmd.ExecuteNonQuery(); 

堆棧跟蹤:

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() 
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) 
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) 
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) 
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 
at xxx.Program.Main(String[] args) in C:\xxx\Program.cs:line 121 

但是我想直接在SQL查詢複製SQL命令並執行它和它的工作。 (它創建的過程)

給你一些背景這裏是代碼

SqlParameter stateName = new SqlParameter(); 
stateName.SqlDbType = SqlDbType.NVarChar; 
stateName.Direction = ParameterDirection.Input; 
stateName.ParameterName = "@Name"; 
stateName.Size = 50; 
Console.Write("Enter state name: "); 
stateName.Value = Console.ReadLine(); 
cmd.Parameters.Add(stateName); 

cmd.CommandText = @"IF EXISTS (SELECT name FROM sysobjects WHERE name='FindState' AND type='P') DROP PROCEDURE FindState"; 
cmd.ExecuteNonQuery(); 
cmd.CommandText = @"CREATE PROCEDURE FindState 
@Name nvarchar(50) 
AS 
SELECT IDState FROM State WHERE [email protected]"; 
cmd.ExecuteNonQuery(); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.CommandText = "FindState"; 
sdr = cmd.ExecuteReader(); 
if (sdr.HasRows) 
{ 
    while (sdr.Read()) 
    { 
    Console.WriteLine("ID state " + stateName.Value + " is " + sdr[0]); 
    } 
} 
else 
{ 
    Console.WriteLine("No such state exists"); 
} 
sdr.Close(); 
+0

沒有erorr消息(帶有堆棧跟蹤),沒有行號和代碼,不清理參數 - 投票關閉。 – TomTom

+0

這個創建過程FindState (@Name nvarchar(50)) AS SELECT IDState FROM State WHERE Name = @ Name請注意存儲的proc參數前後打開和關閉大括號 – CjCoax

+0

試圖在參數周圍放置花括號但它不起作用。 – HomeIsWhereThePcIs

回答

0

我想通了什麼問題了。 在創建過程之前,該參數已添加到SqlCommand。 我上下移動這一行,現在,它的工作原理

cmd.CommandText = @"CREATE PROCEDURE FindState @Name nvarchar(50) AS SELECT IDState FROM State WHERE [email protected]"; 
cmd.ExecuteNonQuery(); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.CommandText = "FindState"; 
cmd.Parameters.Add(stateName); 
sdr = cmd.ExecuteReader(); 
+0

這就是我建議** not **來「回收」'SqlCommand'對象的原因之一 - 每個作業需要使用一個(一個用於「CREATE PROCEDURE」,另一個用於調用該過程) –

0

的有點大段試試這個:

SqlParameter stateName = new SqlParameter(); 
stateName.SqlDbType = SqlDbType.NVarChar; 
stateName.Direction = ParameterDirection.Input; 
stateName.ParameterName = "@Name"; 
stateName.Size = 50; 
Console.Write("Enter state name: "); 
stateName.Value = Console.ReadLine(); 
cmd.Parameters.Add(stateName); 

cmd.CommandText = "IF EXISTS (SELECT name FROM sysobjects WHERE name='FindState' AND type='P') DROP PROCEDURE FindState"; 
cmd.ExecuteNonQuery(); 
cmd.CommandText = "CREATE PROCEDURE FindState 
@Name nvarchar(50) 
AS 
BEGIN 
SELECT IDState FROM State WHERE [email protected]"; 
END 

cmd.CommandType = CommandType.StoredProcedure; 
cmd.ExecuteNonQuery(); 
cmd.CommandText = "FindState"; 

sdr = cmd.ExecuteReader(); 
if (sdr.HasRows) 
{ 
    while (sdr.Read()) 
    { 
    Console.WriteLine("ID state " + stateName.Value + " is " + sdr[0]); 
    } 
} 
else 
{ 
    Console.WriteLine("No such state exists"); 
} 
sdr.Close();