0

これを実行しようとすると、「キーワード「PROCEDURE」の近くの構文が正しくありません」というメッセージが表示されたSqlExceptionが発生します。

120: cmd.CommandText = @"CREATE PROCEDURE FindState @Name nvarchar(50)
     AS SELECT IDState FROM State WHERE Name=@Name";
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 Name=@Name";
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();
4

2 に答える 2

0

私は問題が何であるかを理解しました。プロシージャを作成する前に、パラメータがSqlCommandに追加されました。この行を下に移動すると、機能するようになりました

cmd.CommandText = @"CREATE PROCEDURE FindState @Name nvarchar(50) AS SELECT IDState FROM State WHERE Name=@Name";
cmd.ExecuteNonQuery();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "FindState";
cmd.Parameters.Add(stateName);
sdr = cmd.ExecuteReader();
于 2012-11-09T06:33:03.977 に答える
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 Name=@Name";
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();
于 2012-11-09T06:16:05.643 に答える