我正在使用 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 工具包的模式弹出面板,并在面板中添加了注册和登录表..
我的注册面板:
提前致谢...
最佳答案
我强烈建议您像这样组织代码:
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/