當我嘗試運行這個時,我得到一個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();
沒有erorr消息(帶有堆棧跟蹤),沒有行號和代碼,不清理參數 - 投票關閉。 – TomTom
這個創建過程FindState (@Name nvarchar(50)) AS SELECT IDState FROM State WHERE Name = @ Name請注意存儲的proc參數前後打開和關閉大括號 – CjCoax
試圖在參數周圍放置花括號但它不起作用。 – HomeIsWhereThePcIs