直接将参数赋值为常量0则参数值为null,出现异常:MySql.Data.MySqlClient.MySqlException (0x80004005): Column 'PayType' cannot be null
public static long CreateIntegralPay(long memId, decimal payAmount, decimal buyIntegral)
{
var id = BitConverter.ToInt64(Guid.NewGuid().ToByteArray(), 0);
var sqlBuffer = new StringBuilder();
sqlBuffer.AppendLine("insert into `memberintegralrecordpay` (`Id`,`MemberId`,`PayType`,`PayAmount`,`BuyIntegral`,`PayStatus`,`RecordTime`,`Remark`)");
sqlBuffer.AppendLine("values(@Id, @MemberId, @PayType, @PayAmount, @BuyIntegral, @PayStatus, @RecordTime, @Remark);");
//sqlBuffer.AppendLine("select @@identity; ");
const int val= 0;
var sqlParameters = new MySql.Data.MySqlClient.MySqlParameter[]
{
new MySql.Data.MySqlClient.MySqlParameter("@Id",id),
new MySql.Data.MySqlClient.MySqlParameter("@MemberId",memId),
new MySql.Data.MySqlClient.MySqlParameter("@PayType", 0),
new MySql.Data.MySqlClient.MySqlParameter("@PayAmount", payAmount),
new MySql.Data.MySqlClient.MySqlParameter("@BuyIntegral",buyIntegral),
new MySql.Data.MySqlClient.MySqlParameter("@PayStatus",val),
new MySql.Data.MySqlClient.MySqlParameter("@RecordTime",DateTime.Now),
new MySql.Data.MySqlClient.MySqlParameter("@Remark",string.Empty)
};
if (DbHelper.ExecuteSql(sqlBuffer.ToString(), sqlParameters) > 0)
{
return id;
}
return 0;
}
将0用变量代替后没有问题
public static long CreateIntegralPay(long memId, decimal payAmount, decimal buyIntegral)
{
var id = BitConverter.ToInt64(Guid.NewGuid().ToByteArray(), 0);
var sqlBuffer = new StringBuilder();
sqlBuffer.AppendLine("insert into `memberintegralrecordpay` (`Id`,`MemberId`,`PayType`,`PayAmount`,`BuyIntegral`,`PayStatus`,`RecordTime`,`Remark`)");
sqlBuffer.AppendLine("values(@Id, @MemberId, @PayType, @PayAmount, @BuyIntegral, @PayStatus, @RecordTime, @Remark);");
//sqlBuffer.AppendLine("select @@identity; ");
int val = 0;
var sqlParameters = new MySql.Data.MySqlClient.MySqlParameter[]
{
new MySql.Data.MySqlClient.MySqlParameter("@Id",id),
new MySql.Data.MySqlClient.MySqlParameter("@MemberId",memId),
new MySql.Data.MySqlClient.MySqlParameter("@PayType", val),
new MySql.Data.MySqlClient.MySqlParameter("@PayAmount", payAmount),
new MySql.Data.MySqlClient.MySqlParameter("@BuyIntegral",buyIntegral),
new MySql.Data.MySqlClient.MySqlParameter("@PayStatus",val),
new MySql.Data.MySqlClient.MySqlParameter("@RecordTime",DateTime.Now),
new MySql.Data.MySqlClient.MySqlParameter("@Remark",string.Empty)
};
if (DbHelper.ExecuteSql(sqlBuffer.ToString(), sqlParameters) > 0)
{
return id;
}
return 0;
}