列表框(多个值存储在像sqlserver这样的数据库中) [英] Listbox ( multiple values stored in database like sqlserver)

查看:91
本文介绍了列表框(多个值存储在像sqlserver这样的数据库中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


早安,
我已使用一个列表框,并在listboxx中选择了多个值,我想将这些值存储到数据库中以及如何在asp.net中编写代码,请告诉我代码,请,

在此先感谢
Ganesh


good morning,
i have taken one listbox and am selecting multiple values in listboxx, and i want to store those values into database and how to write the code in asp.net,,please tell me the code,,please,,

Thanks in Advance
Ganesh

推荐答案

请尝试以下操作:

第1步:-在我的情况下,我使用一个ListBox(ListBox1),TextBox(TextBox1)(以显示数据)和一个Button(用于将选择的值保存在数据库中)

Step2 :-在数据库中创建表
Please try this:

Step 1:- In my case I use one ListBox(ListBox1),TextBox(TextBox1)(to show the data) and a Button(for save the seleced values in the database)

Step2:- create a table in the database
create table table2
(
list varchar(max)
)


Step3 :-设置ListBox1的AutoPostBack="True",并在OnSelectedIndexChanged(event)上编写以下代码


Step3:- set the AutoPostBack="True" of the ListBox1 and on the OnSelectedIndexChanged(event) we write the following code

TextBox1.Text = TextBox1.Text + ListBox1.SelectedItem.Text;


Step4 :-现在在按钮上单击Event(用于将值保存在数据库中)


Step4:- Now on the Button Click Event(for save the value in the database)

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
        conn.Open();
        SqlCommand cmd1 = new SqlCommand("insert into table1(list) values('" + TextBox1.Text + "')", conn);
        cmd1.ExecuteNonQuery();
        conn.Close();


注意:->在Web.Config文件中


Note:-> in the Web.Config File

<connectionStrings>
    <add name="con" connectionString="Data Source=my\SQLEXPRESS;Initial Catalog=try;Integrated Security=true;" />
  </connectionStrings>


在Sql Server中编写一个存储过程,以便将值插入表中,然后通过传递适当的参数在您的应用程序中调用该过程.

在Google中搜索上述功能的代码.它非常简单. :)
Write a stored procedure in Sql Server for inserting values into table and then call that procedure in your application by passing appropriate parameters.

Search Google for the code of above functionality. Its very simple. :)




试试这个,


Hi,

Try this,


int[] index = ListBox1.GetSelectedIndices();
for (int i = 0; i < index.Length; i++)
{
   //Database query to update/insert the selected value
   Response.Write(ListBox1.Items[index[i]].Text);
   //ListBox1.Items[index[i]].Text

}



问候,
Suresh



Regards,
Suresh


这篇关于列表框(多个值存储在像sqlserver这样的数据库中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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