将ListBox项存储到数据库中 [英] storing ListBox items into a database

查看:105
本文介绍了将ListBox项存储到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sir如果我想将列表框选中的项目存储到表格的一列(即兴趣)中,那里已经很少有像用户名,id,城市,国家这样的记录,并且我想再添加一列,即兴趣(其中)是一个列表框)和用户在选择一些兴趣时登录。我希望将其添加到特定用户的兴趣栏中,并在另一行中添加。





我试过,用户从列表框中选择的兴趣是存储在一个表中。但它不是用于登录用户。它显示在下一行所有其他列为Null值。我的表格如下:



Sir If i want to store the list-box selected items into one column(i.e interests) of table where already few records are there like username, id,city,country and i want to add one more column i.e interests(which is a list-box) and user who is logged in when select some interests .i want it to be added in that interest column for a particular user, and in a separate row.


I have tried, the interests that user is selecting from list-box is storing in a table.But it not for the logged in user.It is displayed on the next row with all the other columns as Null values.My table looks like this:

UserName       Id      city     country   pin-code       interests
NULL           NULL    NULL     NULL      NULL           privacy,protection 







而不是我想要所有上述用户字段作为登录用户详细信息。




instead I want all the above user fields as the logged on user details.

推荐答案

不存储兴趣作为逗号分隔值。

将每个兴趣存储为新行。



这称为第一范式 [ ^ ]在关系数据库的规范化过程中。
Don't store interests as comma separate values.
Store each interest as a new row.

This is known as first normal form[^] during normalization of a relational database.


我编写了类似的代码这,它存储特定用户的列表框项目,但它正在更新listbox中的项目。喜欢当我选择2项....例如:隐私,php它只显示php ....即当前所选项目。 ...







SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings [RegistrationConnectionString]。 ConnectionString);



SqlCommand cmd = conn.CreateCommand();

I have written code like this, it stores the listbox items for particular user, but it is updating the items in listbox.Like when i select 2 items....for eg: privacy, php its showing only php....i.e the current selected item....



SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);

SqlCommand cmd = conn.CreateCommand();
foreach(ListItem item in ListBoxadditems.Items) // to store the values of listboxitems into database as per logged in user
            {




conn.Open();
                   cmd.CommandText ="UPDATE UserData SET Interests='"+item.Text+"' WHERE UserName='"+Session["New1"]+"'";




cmd.ExecuteNonQuery();
                         Label5.Text = "Data Inserted";
                         conn.Close();



}


}


这篇关于将ListBox项存储到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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