简体   繁体   English

从列表框添加到数据库

[英]Adding from a Listbox to database

I have a text box where I introduce a number and a button named Add which introduce the number in the list box. 我有一个文本框,在其中引入了一个数字,还有一个名为“添加”的按钮,在列表框中引入了该数字。 After adding all the numbers wanted when I click on a button Finalize to add what is in the list box in database. 在添加所有想要的数字后,当我单击“完成”按钮以添加数据库列表框中的内容时。

My database table is Number with ID IDnumber NAME . 我的数据库表是ID IDnumber NAME Number。

IDnumber = when I add multiple numbers from list box in database I want that all numbers have the same ID. IDnumber =当我从数据库的列表框中添加多个数字时,我希望所有数字都具有相同的ID。

For example in my list I have 例如在我的清单中

  • 45341 45341
  • 5466 5466
  • 4646 4646
  • 464 464

and in my database to have 在我的数据库中

     ID   IDnumber     NAME                  

      1        1        45341                 
      1        1         5466            
      1        1        4646             
      1        1         464

And the next time I will add from list box , the next number will have the IDnumber 2. 下次我将从列表框中添加时,下一个数字将具有IDnumber 2。

    <asp:TextBox ID="txtNrShipment" runat="server" Height="16px"  Width="100px"></asp:TextBox>
<asp:Button ID="btnAddSO" runat="server" Text="Add" OnClick="btnAddSO_Click" CausesValidation="False" />
 <asp:ListBox ID="ListBoxSO" runat="server">               
                </asp:ListBox>


<asp:Button ID="btnFinalizeSO" runat="server" Text="Finalize" OnClick="btnFinalizeSO_Click" />



  protected void btnAddSO_Click(object sender, EventArgs e)
    {
        ListBoxSO.Items.Add(txtNrShipment.Text);
        txtNrShipment.Text = " ";
    }

You should read and learn basic ADO.NET to do that, simple insert implementation would be something like this (code not tested, you need to use loop to do what you want, but this example is just a basic insert.): 您应该阅读并学习基本的ADO.NET才能做到这一点,简单的插入实现将是这样的(未经测试的代码,您需要使用loop来完成您想要的事情,但是此示例只是一个基本的插入。):

string connectionString = "yourConnectionString";
string query = "INSERT INTO dbo.YourTable (ID, IDnumber, NAME) " + 
               "VALUES (@Id, @IDnumber, @Name) ";

// create connection and command
using(SqlConnection cn = new SqlConnection(connectionString))
using(SqlCommand cmd = new SqlCommand(query, cn))
{
    // define parameters and their values
    cmd.Parameters.Add("@ID", ID);
    cmd.Parameters.Add("@IDnumber", IDnumber);
    cmd.Parameters.Add("@Name", Name); 

    //open, insert and close
    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();
}

You have not provided the actual code of btnFinalizeSO's click event. 您尚未提供btnFinalizeSO的click事件的实际代码。 If you do not have it yet, the first step you must implement is the connection to your database, for example with Entity Framework. 如果还没有,则必须实现的第一步是与数据库的连接,例如使用Entity Framework。 If you connect with Entity Framework you could create objects of your table from the database to inject them to the db. 如果与Entity Framework连接,则可以从数据库中创建表的对象,以将它们注入db。 For example: 例如:

protected void btnFinalizeSO_Click(object sender, EventArgs e)
{
    using(MyDataBaseEntities db = new MyDataBaseEntities())
   {
       Number lastNumber = (from c in db.Number orderby IDNumber select c).LastOrDefaul();
       foreach(var item in ListBoxSO.Items)
       {

            Number n = new Number();
            n.IdNumber = lastNumber.IdNumber +1; 
            n.Id = lastNumber.Id + 1;
            n.Name = ListBoxSO.GetItemText(item);
            db.Number.Add(n);
       }
      db.SaveChanges();
   }
}

This is more or less what you need. 这或多或少是您所需要的。 May be it is good to catch exceptions. 捕获异常可能很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM