C#SQLCommand - 参数化查询被缩短

时间:2015-09-23 10:13:54

标签: c# sql-server-2012 sqlcommand

为格式化道歉,这有点粗糙

我在C#中通过SQLCommand遇到INSERT语句问题。插入完成后,ID将返回到程序中。

我填充了许多变量并编写了SQLCommand。所有参数都被考虑在内。

using (SqlCommand sqlCatCmd = new SqlCommand())
{
   sqlCatCmd.CommandText = 
       "INSERT INTO [ChargeType](IsRecurring, IsApplied4NewMembers, Name, PurseID, AllowFamilyDiscount) " +
       "Values (@IsRec, @NewMem, @CName, @PurseID, @FamDisc);" +
       "SET @ChrgID = SCOPE_IDENTITY();";

  //Connections are opened further up the code no need to include
  sqlCatCmd.Connection = _importSettings.sqlServerConnection;
  sqlCatCmd.Transaction = _importSettings.sqlServerTransaction;

  //Parameters
  sqlCatCmd.Parameters.AddWithValue("@IsRec", _reapply);
  sqlCatCmd.Parameters.AddWithValue("@NewMem", _onNewMem);
  sqlCatCmd.Parameters.AddWithValue("@CName", _name);
  sqlCatCmd.Parameters.AddWithValue("@PurseID", _purse);
  sqlCatCmd.Parameters.AddWithValue("@FamDisc", _applyFamDisc);

  //Create parameter for newly added chargeType id value
  SqlParameter ChrgTypeIDReturn = new SqlParameter();
  ChrgTypeIDReturn.ParameterName = "@ChrgID";
  ChrgTypeIDReturn.Size = 4;
  ChrgTypeIDReturn.Direction = ParameterDirection.Output;
  sqlCatCmd.Parameters.Add(ChrgTypeIDReturn);

  //execute
  sqlCatCmd.ExecuteNonQuery();

 //update variable with returned id value
 _chrgTypeReturnedId = Convert.ToInt32(ChrgTypeIDReturn.Value);
 _importSettings.sqlServerTransaction.Commit();
 conn.CloseSqlConnection(_importSettings.sqlServerConnection);
}

当我执行查询时,会捕获以下内容:

  

参数化查询'(@IsRec nvarchar(1),@ NewMem nvarchar(1),@ CName nvarchar(25),@ Pur'需要参数'@PurseID',这是未提供的。

似乎是要缩短查询,这里有一个限制吗?看着我的字符串,它似乎在语法上是正确的

由于

2 个答案:

答案 0 :(得分:3)

我认为它不会削减查询,因为它会在异常消息中稍后提到完整的参数名称@PurseID

任何机会检查是否_purse == null。如果是这样,您必须改为使用DBNull.Value

答案 1 :(得分:1)

您可以重写SQL语句以输出插入ID。 喜欢: INSERT INTO table1 (name) OUTPUT Inserted.ID VALUES('FooBar')

然后使用ExecuteScalar()而不是ExecuteNonQuery()。 重写:

using (SqlCommand sqlCatCmd = new SqlCommand())
{
   sqlCatCmd.CommandText = 
       "INSERT INTO [ChargeType](IsRecurring, IsApplied4NewMembers, Name, PurseID, AllowFamilyDiscount) " +
       "OUTPUT Inserted.ID " +
       "Values (@IsRec, @NewMem, @CName, @PurseID, @FamDisc)";

  //Connections are opened further up the code no need to include
  sqlCatCmd.Connection = _importSettings.sqlServerConnection;
  sqlCatCmd.Transaction = _importSettings.sqlServerTransaction;

  //Parameters
  sqlCatCmd.Parameters.AddWithValue("@IsRec", _reapply);
  sqlCatCmd.Parameters.AddWithValue("@NewMem", _onNewMem);
  sqlCatCmd.Parameters.AddWithValue("@CName", _name);
  sqlCatCmd.Parameters.AddWithValue("@PurseID", _purse);
  sqlCatCmd.Parameters.AddWithValue("@FamDisc", _applyFamDisc);

  //ExecuteScalar to get OUTPUT value from Sql string
  int insertedId = sqlCatCmd.ExecuteScalar() as int;

 //update variable with returned id value
 _chrgTypeReturnedId = insertedId;
 _importSettings.sqlServerTransaction.Commit();
 conn.CloseSqlConnection(_importSettings.sqlServerConnection);
}