如何从datagridview的数据保存到SQL Server 2008 R2 [英] How to save data from datagridview to SQL Server 2008 R2
问题描述
我创建一个C#程序,我可以从系统中添加数据并保存到数据库中。我使用的SQL Server 2008 R2。我有2表,名称和地址数据网格视图。我想要做的就是输入我希望它被保存在我的数据库我点击保存按钮后所需要的信息之后。
这是我的数据网格视图的截图:
我在从一个文本框将数据保存到我的数据库代码。但我用在该文本框,我没有对如何使用数据网格视图,而不是文本做同样的处理(将数据保存到我的数据库)的任何想法。请帮忙。谢谢
这是我从一个文本框到我的数据库保存数据使用的代码:
字符串的ConnectionString = ConfigurationManager.ConnectionStrings [的ConnectionString]的ConnectionString。
试
{使用
(SqlConnection的连接=新的SqlConnection(的connectionString))
{
的SqlCommand命令=新的SqlCommand();
command.Connection =连接;
command.CommandText =插入到客户(姓名,地址)的值(@name,@address);
command.Parameters.Add(新的SqlParameter(@名,SqlDbType.VarChar));
command.Parameters.Add(新的SqlParameter(@地址,SqlDbType.VarChar));
command.Parameters [@名]值= name.Text.ToLower();
command.Parameters [@地址]值= address.Text.ToLower()。
connect.Open();
}
}
您需要遍历集合DataGridView.Rows(我假设的DataGridView是无限的)。
使用(SqlConnection的连接=新的SqlConnection(的connectionString ))
{
使用(的SqlCommand命令=新的SqlCommand())
{
command.Connection =连接;
command.CommandText =插入到客户(姓名,地址)的值(@name,@address);
command.Parameters.Add(新的SqlParameter(@名,SqlDbType.VarChar));
command.Parameters.Add(新的SqlParameter(@地址,SqlDbType.VarChar));
connect.Open();
的foreach(的DataGridViewRow行dataGridView1.Rows)
{
如果(!row.IsNewRow)
{
command.Parameters [@名]。值= row.Cells [0]。价值;
command.Parameters [@地址]值= row.Cells [1] .value的;
command.ExecuteNonQuery();
}
}
}
}
I am creating a C# program where I can add data from the system and save to the the database. I am using SQL Server 2008 R2. I have a data grid view which has 2 tables, name and address. What I want to do is after inputting the needed information I want it to be saved in my database after I click the save button.
This is the screenshot of my data grid view:
I have a code in saving data from a textbox into my database. But I used textbox on that and I don't have any idea on how to do the same process(saving data into my database) using data grid view instead of textbox. Please help. Thanks.
This is the code I used in saving data from a textbox to my database.:
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
try
{
using (SqlConnection connect = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand();
command.Connection = connect;
command.CommandText = "insert into customer(name, address) values(@name, @address)";
command.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("@address", SqlDbType.VarChar));
command.Parameters["@name"].Value = name.Text.ToLower();
command.Parameters["@address"].Value = address.Text.ToLower();
connect.Open();
}
}
You need to iterate the DataGridView.Rows collection (I assume the DataGridView is unbounded).
using (SqlConnection connect = new SqlConnection(connectionString))
{
using(SqlCommand command = new SqlCommand())
{
command.Connection = connect;
command.CommandText = "insert into customer(name, address) values(@name, @address)";
command.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("@address", SqlDbType.VarChar));
connect.Open();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if(!row.IsNewRow)
{
command.Parameters["@name"].Value = row.Cells[0].Value;
command.Parameters["@address"].Value = row.Cells[1].Value;
command.ExecuteNonQuery();
}
}
}
}
这篇关于如何从datagridview的数据保存到SQL Server 2008 R2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!