为什么我的代码只插入一个条目? [英] Why does my code inserts only one entry?

查看:69
本文介绍了为什么我的代码只插入一个条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的数据库中插入条目,我只设法插入第一个条目。我正在使用LINQ并且不知道我做错了什么:



这里是我的代码:



I am trying to insert entries in my database, i only managed to insert first entry. I am using LINQ and don't have any idea what I did wrong:

here is my code:

User newUser = new User();

newUser.FirstName = firstname;
newUser.LastName = lastname;
newUser.UserName = username;
newUser.Email = email;
newUser.Cell = cellno;
newUser.Password = password;
newUser.UserLevel = 1;
newUser.Active = 1; 

see.Users.InsertOnSubmit(newUser);

try
{
    see.SubmitChanges();
}
catch
{
    return false;
}

int locid = registerLocation(latitude, longitude);  //same as the current method
int id = newUser.UserId;

  registerCustomer(id, locid);
}



然后这是registerCustomer方法():




and then this is registerCustomer Method():

Customer cus = new Customer();
cus.DefaultLocation = locationId;
cus.UserId = userid;
see.Customers.InsertOnSubmit(cus);

try
{
    see.SubmitChanges();
}
catch
{
    return false;
}

推荐答案

Hi Mathale,



不是了解你正在循环代码或者只执行一次。

如果它运行一次,那么它的绝对单项将进入数据库。



Pelase找到以下代码

Hi Mathale ,

Its not understood that you are looping the code or this execute only once .
If its runs one time then its definitely single entry will inser into database .

Pelase find below code
protected void Button1_Click(object sender, EventArgs e)
   {
       SaveCustomerInfo();
   }

   private void SaveCustomerInfo()
   {
       using (NorthwindDataContext context = new NorthwindDataContext())
       {
           //Create a new instance of the Customer object
           Customer cust = new Customer();
           //Add new values to each fields
           cust.CustomerID = TextBoxID.Text;
           cust.CompanyName = TextBoxCompanyName.Text;
           cust.ContactName = TextBoxContactName.Text;
           cust.ContactTitle = TextBoxContactTitle.Text;
           cust.Address = TextBoxAddress.Text;
           cust.City = TextBoxCity.Text;
           cust.Region = TextBoxRegion.Text;
           cust.PostalCode = TextBoxPostalCode.Text;
           cust.Country = TextBoxCountry.Text;

           //Insert the new Customer object
           context.Customers.InsertOnSubmit(cust);
           //Sumbit changes to the database
           context.SubmitChanges();

           //Display Message for successful operation
           LiteralMessage.Text = "<p style='color:Green;'>Information Successfully saved!</p>";
       }
   }


这篇关于为什么我的代码只插入一个条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆