【发布时间】:2015-03-24 15:57:02
【问题描述】:
我有一个网格视图,我用 C# 填充它,用户对其进行编辑,然后我将它插入到不同的表中,当我插入它时,它会接受用户在第一行中所做的更改并忽略其中的所有 chagnes其他行。 C3 列是用户编辑的列
这是数据网格生成查询
string output = "select distinct [C1],C2 as [Error_Name], '' as [C3], [C4],[C5] from rejection";
SqlCommand cmd2 = new SqlCommand();
cmd2.CommandType = CommandType.Text;
cmd2.CommandText = output;
cmd2.Connection = conne;
conne.Open();
SqlDataAdapter dscmd = new SqlDataAdapter(output, strconnection);
DataSet ds = new DataSet();
dscmd.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
这是插入查询
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string StrQuery = @"INSERT INTO [test2] VALUES ('" + dataGridView1.Rows[i].Cells[0].Value + "', '" + dataGridView1.Rows[i].Cells[1].Value + "', '" + dataGridView1.Rows[i].Cells[2].Value + "', '" + dataGridView1.Rows[i].Cells[3].Value + "', '" + dataGridView1.Rows[i].Cells[4].Value + "');";
SqlConnection conn = new SqlConnection(strconnection);
conn.Open();
using (SqlCommand comm = new SqlCommand(StrQuery, conn))
{
comm.ExecuteNonQuery();
}
conn.Close();
请注意,所有行都插入到数据库中,但第一行只有编辑更改,所有其他行都没有编辑更改,它们是相同的。
当我使用这个连接字符串时:
string strconnection = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\ksa\Documents\Visual Studio 2012\Projects\test\test\test.mdf;Integrated Security=True";
它可以工作......但是当我使用 app.sitting 时它:
string strconnection = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
在应用程序中坐着:
<connectionStrings>
<add name="test" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\test.mdf;Integrated Security=True" providerName="Microsoft.SqlServerCe.Client.4.0" />
</connectionStrings>
没用!!
【问题讨论】:
-
关于SQL Injection的强制性评论。
-
你真正想要什么?
-
@KhurramAli 我想要的是从表中选择 3 列,用户将手动逐行添加第 4 列,然后他将时钟更新按钮,并将插入所有 3 列,包括由编辑的列用户到另一个表,现在发生的是更新或第一行中的更改仅出现在表中,并且重置与来自第一个表的重置相同,忽略对它们所做的所有更改
-
使用相关代码编辑您的问题
-
你是什么意思,问题上的代码是从数据库到gridview的select和从grid view到数据库的插入,你还需要什么代码
标签: c# sql winforms datagridview edit