出错代码:
public bool Delete_List(int cID)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("delete from Emp");
strSql.Append(" where C_Id = @C_Id");
SqlParameter[] parameters = {
new SqlParameter("@C_Id",SqlDbType.Int,)};
parameters[].Value = cID;
int rows = Helper.SQLHelper.ExcuteSQL(strSql.ToString());
if (rows > )
{
return true;
}
else
{
return false;
}
}
出错原因:
没有在执行sql语句的ExcuteSQL函数中申明标量变量
修改方法: int rows = Helper.SQLHelper.ExcuteSQL(strSql.ToString(),parameters);
将ExcuteSQL函数改为:
/// <summary>
/// 执行SQL语句,返回受影响的记录数
/// </summary>
/// <param name="strSql">SQL语句</param>
/// <param name="cmdParms"></param>
/// <returns>受影响的行数</returns>
public static int ExcuteSQL(string SQLString, params SqlParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(GetConnectionString()))//连接数据库
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);//关联SQL语句
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
throw e;
}
}
}
}