c# - 使用 Linq asp.net c# 在 InsertOnSubmit() 的两个不同表中插入数据

标签 c# asp.net mysql database-design linq-to-sql

我正在使用 asp.net c# 和 Mysql 进行在线购物项目。我正在使用 Devart Linqconnect (LinqtoMysql)。我在 mysql 客户和客户地址中有两个表:

客户表
CustomerID 整型
Customerhone varchar(20);
客户密码 Varchar(20);
电子邮件 varchar(20)
名字字符 (50)
姓氏(50)
用户名 varshar(50)

Customer_Addresses
Customer_address_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
Customer_ID INT NOT NULL,
地址 1 CHAR(250),
地址2 CHAR(250),
城市 CHAR(20),
状态 CHAR(20), 密码 VARCHAR(20),
主键(Customer_address_ID),
外键(Customer_ID)引用客户(Customer_ID)

当我使用 LINQ to mysql 编写有关客户注册的代码时:

 using (ShoppingDataContext data = new ShoppingDataContext())
        {
            Customer NewCustomer = new Customer();
            CustomerAddress newaddress = new CustomerAddress();
            newaddress.CustomerID = NewCustomer.CustomerID;
            NewCustomer.CustomerFirstname = TextBoxFirstName.Text;
            NewCustomer.CustomerLastname = TextBoxLastname.Text;
            NewCustomer.CustomerEmail = TextBoxEmail.Text;
            NewCustomer.Username = TextBoxusername.Text;
            NewCustomer.CustomerPassword = TextBoxPassword.Text;
            newaddress.Address1 = TextBoxAddress1.Text;
            newaddress.Address2 = TextBoxAddress2.Text;
            newaddress.City = TextBoxCity.Text;
            newaddress.State = TextBoxState.Text;
            newaddress.Pincode = TextBoxPincode.Text;
            System.Web.Security.Membership.CreateUser(TextBoxusername.Text, TextBoxPassword.Text);
            data.Customers.InsertOnSubmit(NewCustomer);
            PanelRegister.Visible = false;
            ConfimPanel.Visible = true;

        }

此代码是否可以将数据插入到两个表中。请帮忙。 customer_address表能否根据customerId为外键检测出输入的客户地址是customer表的..

还有一件事我正在使用 ajax 工具包的模式弹出面板,并在面板中添加了注册和登录表..

我的注册面板: enter image description here

提前致谢...

最佳答案

我强烈建议您像这样组织代码:

 using (ShoppingDataContext data = new ShoppingDataContext())
 {
     Customer newCustomer = new Customer()
     {
         CustomerFirstname = TextBoxFirstName.Text,
         CustomerLastname = TextBoxLastname.Text,
         CustomerEmail = TextBoxEmail.Text,
         Username = TextBoxusername.Text,
         CustomerPassword = TextBoxPassword.Text
     };

     //now I'd like to be proven wrong here, but I believe you need to insert
     //and submit at this point
     data.Customers.InsertOnSubmit(newCustomer);
     data.SubmitChanges();

     CustomerAddress newaddress = new CustomerAddress()
     {
          CustomerID = NewCustomer.CustomerID,
          Address1 = TextBoxAddress1.Text,
          Address2 = TextBoxAddress2.Text,
          City = TextBoxCity.Text,
          State = TextBoxState.Text,
          Pincode = TextBoxPincode.Text,
     };
     //add new address to your customer and save
     newCustomer.CustomerAddresses.Add(newAddress);
     //it has been a while since I used linq2sql so you may need one of these:
     //newCustomer.CustomerAddresses.InsertOnSubmit(newAddress);
     //newCustomer.CustomerAddresses.Attach(newAddress);
     //basically use intellisense to help you figure out the right one, you might
     //have some trial and error here
     data.SubmitChanges();

     System.Web.Security.Membership.CreateUser(TextBoxusername.Text, TextBoxPassword.Text);

     PanelRegister.Visible = false;
     ConfimPanel.Visible = true;

  }

另外,请注意插入和 db.SubmitChanges() 被移动到的位置。现在我不确定您是否可以使用一个 SubmitChanges() 一次完成此操作,或者您是否需要如图所示的两个。我很想知道你对此的最新情况。

您可以在此处查看一个简单的插入示例:

http://msdn.microsoft.com/en-us/library/bb386941.aspx

编辑:您可能希望将其全部包装在事务范围中,以便它作为一个单元成功或失败

using(TransactionScope scope = new TransactionScope())
{
  using(ShoppingDataContext data = new ShoppingDataContext())
  {
    //the rest of your code
  }
}

只是一个想法。

关于c# - 使用 Linq asp.net c# 在 InsertOnSubmit() 的两个不同表中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15452565/

相关文章:

c# - WCF ServiceContract 中的筛选方法

c# - 如果关联的 UI 被释放,C# 中的异步可等待代码会发生什么情况

c# - 正则表达式:匹配 JavaScript 源代码中的 'in' 运算符

asp.net - 在 IE7 中使用 ASP.Net、CSS、VS 2008 的奇怪定位行为

Asp.net treview 图像填充

c# - ASP.NET C# Ajax 调用错误

mysql - 从两个连接表更新单个表 (SQL)

mysql - 当 MySQL 中的自增主键达到最大值时会发生什么

c# - 我们应该在调用异步回调的库中使用 ConfigureAwait(false) 吗?

php - 如何在本地 apache (php) 中设置 aws DB 常量?