如何将列表框项保存到多个数据库字段?喜欢拆分

时间:2015-03-17 01:24:27

标签: c# asp.net listbox

我正在使用Listbox1和listbox2现在我想将所选项目从listbox1传输到listbox2,它们是(ID,firstname middlename lastname)所以在我的数据库中,firstname和middlename以及lastname在不同的字段中,所以在我转发listbox1之后listbox2的项目我想将它保存到数据库,但问题是...我不知道如何拆分这些单词并给每个单词一个字符串名称命令它们保存到指定的字段

这是我的sqlconnection代码

void database1()
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select * From [User_TBL_DB]", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        conn.Close();
        DataTable myDataTable = ds.Tables[0];
        DataRow tempRow = null;

        foreach (DataRow tempRow_Variable in myDataTable.Rows)
        {
            tempRow = tempRow_Variable;
            ListBox2.Items.Add((tempRow["ID"] + ", " + tempRow["Firstname"] + " " + " " + tempRow["Middlename"] + " " + " " + tempRow["Lastname"]));
        }
    }

然后我将这些项目从listbox1转移到listbox2

 protected void Button2_Click(object sender, EventArgs e)
    {
        for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
        {
            if (ListBox2.Items[i].Selected)
            {
                ListBox3.Items.Add(ListBox2.Items[i]);
                ListBox2.Items.RemoveAt(i);
            }


        }
    }

现在要保存..我不知道..

在我从listbox1将项目传输到listbox2之后,该项目将在保存后被删除到listbox1的数据库中。

listbox1数据库名称是

 [User_TBL_DB] 
fields: ID,Firstname,Middlename,lastname

listbox2数据库名称是

[CopyUser_TBL_DB] 
fields: ID,Firstname,Middlename,lastname

这是当前的输出

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您只想分割单词并将其添加到db,那么请执行

protected void Button2_Click(object sender, EventArgs e)
{
    for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
    {
        if (ListBox2.Items[i].Selected)
        {
            ListBox3.Items.Add(ListBox2.Items[i]);
            string[] splits = ListBox2.Items[i].ToString().Split(new char[]{' ', ','}); // For splitting both empty space and comma (you've used in ur question)
            SqlCommand cmd = new SqlCommand("Insert Into [CopyUser_TBL_DB] ([ID], [Firstname], [Middlename], [Lastname]) Values (@ID, @FName, @MName, @LName)", conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(splits[0]);
            cmd.Parameters.Add("@FName", SqlDbType.VarChar).Value = splits[1];
            cmd.Parameters.Add("@MName", SqlDbType.VarChar).Value = splits[2];
            cmd.Parameters.Add("@LName", SqlDbType.VarChar).Value = splits[3];
            cmd.ExecuteNonQuery();
            ListBox2.Items.RemoveAt(i);
        }
    }
}

如果有人在名字中输入空格,并且不是一个好方法,则会失败。

一种好方法是创建一个类并在其中存储详细信息。

class UserDetails
    {
        public string ID { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public string FullRowDisplay { get; set; }

        public UserDetails(string id, string fName, string mName, string lName)
        {
            ID = id;
            FirstName = fName;
            MiddleName = mName;
            LastName = lName;
            FullRowDisplay = id + " " + fName + " " + mName + " " + lName;
        }

    }

并在阅读方面

ListBox1.DisplayMember = ListBox2.DisplayMember = "FullRowDisplay";
foreach (DataRow tempRow_Variable in myDataTable.Rows)
    {
        tempRow = tempRow_Variable;
        ListBox2.Items.Add(new UserDetails(tempRow["ID"], tempRow["Firstname"], tempRow["Middlename"], tempRow["Lastname"]));
    }

并在插入部分

if (ListBox2.Items[i].Selected)
    {
        UserDetails udetail = ListBox2.Items[i];
        ListBox3.Items.Add(udetail);
        SqlCommand cmd = new SqlCommand("Insert Into [CopyUser_TBL_DB] ([ID], [Firstname], [Middlename], [Lastname]) Values (@ID, @FName, @MName, @LName)", conn);
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(udetail.ID);
        cmd.Parameters.Add("@FName", SqlDbType.VarChar).Value = udetail.FirstName;
        cmd.Parameters.Add("@MName", SqlDbType.VarChar).Value = udetail.MiddleName;
        cmd.Parameters.Add("@LName", SqlDbType.VarChar).Value = udetail.LastName;
        cmd.ExecuteNonQuery();
        ListBox2.Items.RemoveAt(i);
    }