[英]ASP.NET insert from bll to mysql
I am trying to make an insert to an MySql database using a three layer solution (or what it might be called). 我正在尝试使用三层解决方案(或可能被称为)插入MySql数据库。
I have done this may times with an MS-sql database and it has worked very well. 我可能使用MS-sql数据库来完成此操作,并且效果很好。
But now when I am trying to make an insert I get the the ID can't be null. 但是现在当我尝试插入时,我得到的ID不能为null。 I thought the database took care of that. 我以为数据库可以解决这个问题。 If I write an insert directly in the code and use the MySqlCommand and executeNonQuery it works great. 如果我直接在代码中编写插入并使用MySqlCommand和executeNonQuery,则效果很好。
Is it not possible to use BLL and DAL with MySql? 不能在MySql中使用BLL和DAL?
Error message: 错误信息:
System.Data.NoNullAllowedException: Column 'GiftID' does not allow nulls. System.Data.NoNullAllowedException:列'GiftID'不允许为空。 at System.Data.DataColumn.CheckNullable(DataRow row) at System.Data.DataColumn.CheckColumnConstraint(DataRow row, DataRowAction action) at System.Data.DataTable.RaiseRowChanging(DataRowChangeEventArgs args, DataRow eRow, DataRowAction eAction, Boolean fireEvent) at System.Data.DataTable.SetNewRecordWorker(DataRow row, Int32 proposedRecord, DataRowAction action, Boolean isInMerge, Int32 position, Boolean fireEvent, Exception& deferredException) at System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32 pos, Boolean fireEvent) at System.Data.DataRowCollection.Add(DataRow row) at PayEx.payexusersDataTable.AddpayexusersRow(payexusersRow row) in c:\\Users\\IT\\AppData\\Local\\Temp\\Temporary ASP.NET Files\\payex\\45bd406a\\10c84208\\App_Code.cyqhjqo7.1.cs:line 444 at PayExBLL.AddPayExUser(String Firstname, String Lastname, String Company, String Address, String Zip, String City, String Phone, String Email, Byte ContactMe, UInt32 Amount, UInt32 TransactionNumber, Byte Anonymous, String Cu 在System.Data.DataTable.RaiseRowChanging(DataRowChangeEventArgs args,DataRow eRow,DataRowAction eAction,Boolean fireEvent)在System.Data.DataColumn.CheckNullable(DataRow行)在System.Data.DataColumn.CheckColumnConstraint(DataRow行,DataRowAction操作) .Data.DataTable.SetNewRecordWorker(DataRow行,Int32推荐记录,DataRowAction操作,布尔值isInMerge,Int32位置,布尔型fireEvent,Exception&deferredException)位于System.Data.DataTable.InsertRow(DataRow行,Int32提议ID,Int32 pos,布尔型FireEvent) PayEx.payexusersDataTable.AddpayexusersRow(payexusersRow行)的System.Data.DataRowCollection.Add(DataRow行)位于c:\\ Users \\ IT \\ AppData \\ Local \\ Temp \\ Temporary ASP.NET Files \\ payex \\ 45bd406a \\ 10c84208 \\ App_Code.cyqhjqo7 .1.cs:PayExBLL.AddPayExUser处的第444行(字符串名,字符串姓氏,字符串公司,字符串地址,字符串邮政编码,字符串城市,字符串电话,字符串电子邮件,字节ContactMe,UInt32金额,UInt32 TransactionNumber,字节匿名,字符串Cu rrency) in c:\\Users\\IT\\Documents\\Visual Studio 2008\\WebSites\\payex\\App_Code\\BLL\\PayExBLL.cs:line 66 at _Default.btn_next3_Click(Object sender, EventArgs e) in c:\\Users\\IT\\Documents\\Visual Studio 2008\\WebSites\\payex\\Default.aspx.cs:line 191 rrency)在c:\\ Users \\ IT \\ Documents中的_Default.btn_next3_Click(Object sender,EventArgs e)的c:\\ Users \\ IT \\ Documents \\ Visual Studio 2008 \\ WebSites \\ payex \\ App_Code \\ BLL \\ PayExBLL.cs:行66 \\ Visual Studio 2008 \\ WebSites \\ payex \\ Default.aspx.cs:第191行
My code: 我的代码:
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
public bool AddPayExUser(string Firstname, string Lastname, string Company, string Address, string Zip, string City, string Phone, string Email, byte ContactMe, uint Amount, uint TransactionNumber, byte Anonymous, string Currency)
{
PayEx.payexusersDataTable puTable = new PayEx.payexusersDataTable();
PayEx.payexusersRow puRow = puTable.NewpayexusersRow();
puRow.Firstname = Firstname;
puRow.Lastname = Lastname;
puRow.Company = Company;
puRow.Address = Address;
puRow.Zip = Zip;
puRow.City = City;
puRow.Phone = Phone;
puRow.Email = Email;
puRow.ContactMe = ContactMe;
puRow.Amount = Amount;
puRow.TransactionNumber = TransactionNumber;
puRow.Anonymous = Anonymous;
puRow.Currency = Currency;
puTable.AddpayexusersRow(puRow);
int rowsAffected = Adapter.Update(puTable);
return rowsAffected == 1;
}
System.Data.NoNullAllowedException: Column 'GiftID' does not allow nulls. at
From the looks of your code, you're forgetting to pass in a GiftID
parameter to your function but it's expected (and can't be null) in your table row. 从代码的外观来看,您忘记了将GiftID
参数传递给函数,但是它在表行中是预期的(不能为null)。
Hence the exception. 因此,例外。 Either set it in your code above, or define a default value on it in your MySQL database. 您可以在上面的代码中进行设置,也可以在MySQL数据库中为其定义默认值。
EDIT: This comment assumes you're inserting into a table for which GiftId is the primary key, which seems unlikely. 编辑:此注释假定您要插入一个表,其中GiftId是主键,这似乎不太可能。 If so, Eoin Campbell's answer makes more sense! 如果是这样,Eoin Campbell的答案就更有意义了!
Check if GiftId is an AUTO_INCREMENT column; 检查GiftId是否为AUTO_INCREMENT列; that's MySQL's equivalent of identity. 这相当于MySQL的身份。
You can recreate the column as identity like: 您可以将列重新创建为标识,例如:
ALTER TABLE Gifts
DROP COLUMN GiftId;
ALTER TABLE items
ADD COLUMN GiftId INT NOT NULL AUTO_INCREMENT FIRST;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.