在SQL数据库中存储ListView数据 [英] Store listview data at sql database
问题描述
亲爱的所有人,
在这里,我使用Visual Studio 2008和SQL Management Studio Express处理Windows应用程序.我有一个listview,一个有3列,在该列下有一些数据.但是我不知道该怎么做.欢迎所有回复....
在此先感谢
Dhinesh Kumar.V
我尝试过这个
Dear All,
Here i working with windows application using visual studio 2008 and sql management studio express. I have one listview and that one having 3 columns and some data below that columns. But i dont know how to done this one.. All replies welcome....
Thanks in Advance
Dhinesh Kumar.V
I tried this one
for (int i = 0; i < listView1.Items[i].Checked; i++)
{
SqlConnection _sqlconnection = new SqlConnection();
string[] conString1 = DBSettings.connectionStringValues();
string connectionString1 = string.Format(@"server=" + conString1[0] + ";database=POSNET_ROOM;uid=" + conString1[2] + ";pwd=" + conString1[3]);
_sqlconnection.ConnectionString = connectionString1;
_sqlconnection.Open();
// SqlCommand _sqlcommand = new SqlCommand("insert into Listview (RoomType,RoomNumber,GuestName,EnteredDate)values('"+listView1.Items[i].SubItems[0].Text+"','"+listView1.Items[i].SubItems[1].Text+"','"+listView1.Items[i].SubItems[2].Text+"','"+listView1.Items[i].SubItems[3].Text+"')",_sqlconnection);
// SqlCommand _sqlcommand = new SqlCommand("insert into Listview (RoomType,RoomNumber,GuestName,EnteredDate)values('" + listView1.Items[1].SubItems[0].Text + "','" + listView1.Items[1].SubItems[1].Text + "','" + listView1.Items[1].SubItems[2].Text + "','" + listView1.Items[1].SubItems[3].Text + "')", _sqlconnection);
SqlDataAdapter _sqldataadapter = new SqlDataAdapter("insert into Listview (RoomType,RoomNumber,GuestName,EnteredDate)values('"+listView1.Items[i].SubItems[0].Text+"','"+listView1.Items[i].SubItems[1].Text+"','"+listView1.Items[i].SubItems[2].Text+"','"+listView1.Items[i].SubItems[3].Text+"')",_sqlconnection);
// SqlDataReader _sqldatareader = _sqlcommand.ExecuteReader();
_sqldatareader.Read();
}
推荐答案
^ ]
如何从ListView保存数据到SQL数据库 [
save listview data to sql databse[^]
How to save data from ListView to SQL Database[^]
如果要保存列表视图中选中的项目,可以使用 ListView.CheckedItems属性 [
If you want to save the checked items of a list view, you can loop through them using ListView.CheckedItems Property [^]. That should make the code more robust.
So:
- prepare the SQL statement
- loop through the checked items
- on each iteration set the parameters correctly based on the item in hand
- execute the SQL statement (INSERT
)
Few things to notice:
- use a transaction for the whole operation
- possibly delete all previously inserted data
- utilize proper error handling and roll back the transaction in case of an error
- never ever concatenate the values, useSqlParameter
in your statements
for (int i = 0; i < listView1.Items.Count; i++)
{
SqlConnection _sqlconnection = new SqlConnection();
string[] conString1 = DBSettings.connectionStringValues();
string connectionString1 = string.Format(@"server=" + conString1[0] + ";database=POSNET_ROOM;uid=" + conString1[2] + ";pwd=" + conString1[3]);
_sqlconnection.ConnectionString = connectionString1;
_sqlconnection.Open();
SqlCommand _sqlcommand = new SqlCommand("insert into Listview (RoomType,RoomNumber,GuestName,EnteredDate)values(''"+listView1.Items[i].SubItems[0].Text+"'',''"+listView1.Items[i].SubItems[1].Text+"'',''"+listView1.Items[i].SubItems[2].Text+"'',''"+listView1.Items[i].SubItems[3].Text+"'')",_sqlconnection);
SqlDataReader _sqldatareader = _sqlcommand.ExecuteReader();
_sqldatareader.Read();
在这里listView1-> Listview的名称
列表视图->表名
RoomType,RoomNumber,GuestName,EnteredDate-> Table和listview的列
Here listView1 -> Name of Listview
Listview -> Table Name
RoomType,RoomNumber,GuestName,EnteredDate -> Columns of Table and listview
这篇关于在SQL数据库中存储ListView数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!