SqlServer中的列表框数据 [英] Listbox data in SqlServer
问题描述
你好,
我在一个有5个列表框的项目中工作,我想要的是必须将所有这5个列表框的数据添加到数据库中,因为它们现在是一个文本框,我正在其中维护产品ID,我想要所有这5个列表框的数据该单一产品ID下的列表框.我试过的是这个
hello,
I am working in a project where i have 5 listboxes and what i want is that the data of all those 5 listboxes must be added into the database now their is a textbox where i am maintaining the product id i want the data of all those 5 listboxes under that single product id. what i tried is this
public static string getconnection()
{
return "Data Source=.;Initial Catalog=master;Integrated Security=True";
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(getconnection());
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO dummy value(@myvalues)", con);
foreach (object i in listBox1.Items.ToString())
{
cmd.Parameters.AddWithValue("@myvalues", i.ToString());
cmd.ExecuteNonQuery();
}
con.Close();
MessageBox.Show("done...");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
请帮帮我,因为我需要这个项目...
谢谢...
基数:rose:
Please help me out as i require this for my project...
Thank you...
Radix :rose:
推荐答案
好,您需要将此行移到foreach循环中:
Well, you need to move this line into the foreach loop:
SqlCommand cmd = new SqlCommand("INSERT INTO dummy value(@myvalues)", con);
...是因为您每次执行循环时都会在现有的SqlCommand
对象中添加一个新参数.假设其他一切都还好,我猜想这是第一次通过循环,但是此后每个循环都会失败.
顺便说一句,您应该在try/catch
中添加一个finally
块,以便无论发生什么情况,连接都将关闭.这就是我编写相同方法的方式:
... because what you''re doing is adding a new parameter to the existing SqlCommand
object each time you run through the loop. Assuming everything else is okay, my guess is that the first time through the loop works, but it fails for every one after that.
And by the way, you should add a finally
block to your try/catch
so that no matter what happens, the connection is closed. This is the way I would have written the same method:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = null;
SqlCommand cmd = null;
try
{
con = new SqlConnection(getconnection());
con.Open();
foreach (object i in listBox1.Items.ToString())
{
cmd = new SqlCommand("INSERT INTO dummy value(@myvalues)", con);
cmd.Parameters.AddWithValue("@myvalues", i.ToString());
cmd.ExecuteNonQuery();
}
MessageBox.Show("done...");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con != null)
{
con.Close();
}
}
}
Smith先生,我尝试过您的代码,但它向数据库中插入了一些错误的值,我复制了您的代码并将其粘贴到我的butto1_click事件中,这是我按下按钮时的默认值在数据库中是
System.Windows.Forms.Listbox.Items
以数组形式包含表格的每一行中的每个字符,如果我做错了,请纠正我
谢谢&问候
基数.
====================
来自JSOP:嗯,也许我给您的答案是错误的,因为您发布的问题所包含的信息不足.那没有理由用1来投票给我答案.也许您代码的其他部分甚至比您发布的方法所破坏的更多.尝试使用调试器.
Smith sir i tried ur code but it inserts some wrong value into the database i copied ur code and pasted it in my butto1_click event the default value at hit of my button which goes in the database is
System.Windows.Forms.Listbox.Items
in an array form with each character in each row of my table, do rectify me if i am doing some mistake
Thanks & Regards
Radix.
=====================
From JSOP: Well, maybe I gave you the wrong answer because you posted a question that didn''t contain nearly enough info. That''s no reason to vote my answer with a 1. Maybe other parts of your code are even more broken than the methods you posted indicate. Try using the debugger.
这篇关于SqlServer中的列表框数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!