直接將參數賦值爲常量0則參數值爲null,出現異常:MySql.Data.MySqlClient.MySqlException (0x80004005): Column 'PayType' cannot be nullsql
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用變量代替後沒有問題ide
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; }