ASP.NET从bll插入到mysql

发布于 2024-07-20 15:42:42 字数 2417 浏览 6 评论 0原文

我正在尝试使用三层解决方案(或可能被称为的解决方案)插入 MySql 数据库。

我已经用 MS-sql 数据库做过很多次了,效果非常好。

但现在当我尝试插入时,我得到 ID 不能为空。 我认为数据库已经解决了这个问题。 如果我直接在代码中编写插入并使用 MySqlCommand 和executeNonQuery,效果会很好。

MySql 不能使用 BLL 和 DAL 吗?

错误消息:

System.Data.NoNullAllowedException:列“GiftID”不允许为空。 在System.Data.DataColumn.CheckNullable(DataRow行)在System.Data.DataColumn.CheckColumnConstraint(DataRow行,DataRowAction操作)在System.Data.DataTable.RaiseRowChanging(DataRowChangeEventArgs args,DataRow eRow,DataRowAction eAction,Boolean fireEvent)在系统.Data.DataTable.SetNewRecordWorker(DataRow row,Int32 suggestRecord,DataRowAction 操作,Boolean isInMerge,Int32 位置,Boolean fireEvent,Exception& deferredException) 在 System.Data.DataTable.InsertRow(DataRow row,Int32 suggestID,Int32 pos,Boolean fireEvent)在 c:\Users\IT\AppData\Local\Temp\Temporary ASP.NET Files\payex\45bd406a\10c84208\App_Code 中 PayEx.payexusersDataTable.AddpayexusersRow(payexusersRow 行)的 System.Data.DataRowCollection.Add(DataRow 行)。 cyqhjqo7.1.cs:PayExBLL.AddPayExUser 处的第 444 行(字符串名字、字符串姓氏、字符串公司、字符串地址、字符串邮政编码、字符串城市、字符串电话、字符串电子邮件、字节 ContactMe、UInt32 金额、UInt32 TransactionNumber、字节匿名、字符串货币)在c:\ Users \ IT \ Documents \ Visual Studio 2008 \ WebSites \ payex \ App_Code \ BLL \ PayExBLL.cs:第66行在_Default.btn_next3_Click(对象发送者,EventArgs e)在c:\ Users \ IT \ Documents \Visual Studio 2008\WebSites\payex\Default.aspx.cs:第 191 行

我的代码:

[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;
}

I am trying to make an insert to an MySql database using a three layer solution (or what it might be called).

I have done this may times with an MS-sql database and it has worked very well.

But now when I am trying to make an insert I get the the ID can't be 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.

Is it not possible to use BLL and DAL with MySql?

Error message:

System.Data.NoNullAllowedException: Column 'GiftID' does not allow nulls. 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 Currency) 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

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日记撕了你也走了 2024-07-27 15:42:42
System.Data.NoNullAllowedException: Column 'GiftID' does not allow nulls. at 

从代码的外观来看,您忘记将 GiftID 参数传递给函数,但它在表行中是预期的(并且不能为空)。

因此例外。 要么在上面的代码中设置它,要么在 MySQL 数据库中定义它的默认值。

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.

Hence the exception. Either set it in your code above, or define a default value on it in your MySQL database.

∞梦里开花 2024-07-27 15:42:42

编辑:此评论假设您要插入一个以 GiftId 为主键的表,这似乎不太可能。 如果是这样,Eoin Campbell 的回答就更有意义了!

检查 GiftId 是否为 AUTO_INCRMENT 列; 这相当于 MySQL 的身份。

您可以将列重新创建为标识,例如:

ALTER TABLE Gifts
         DROP COLUMN GiftId;

ALTER TABLE items
         ADD COLUMN GiftId INT NOT NULL AUTO_INCREMENT FIRST;

EDIT: This comment assumes you're inserting into a table for which GiftId is the primary key, which seems unlikely. If so, Eoin Campbell's answer makes more sense!

Check if GiftId is an AUTO_INCREMENT column; that's MySQL's equivalent of identity.

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文