使用SQL查询更新datagridview [英] Update datagridview with SQL query
本文介绍了使用SQL查询更新datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据网格,并使用sql命令对此进行更新:
Hi,
I have a data grid and update this with sql command:
string str = @"update dbo." + tblname + "";
str += " set SettingValue = " + value + "";
str += " where id = ''" + id + "''";
SqlCommand cm = new SqlCommand(str, MyConnection);
MyConnection.Open();
SqlDataReader re;
re = cm.ExecuteReader();
re.Close();
MyConnection.Close();
BindGrid();
并从datagridview中为"tblname","id"和"value"提供数据:
and give data from datagridview for ''tblname'' , ''id'' and ''value'':
int Id =Convert.ToInt32(dataGridView1.CurrentRow.Cells["id"].Value);
string tblname = dataGridView1.CurrentRow.Cells["TableName"].Value.ToString();
string val = textBox1.Text;
MyUpdate(tbl, val, Id);
这是对Bind Datagridview的查询:
and this is query for Bind Datagridview:
private void BindGrid()
{
dataGridView1.DataSource = bindingSource1;
var str = @"SELECT B.Id, CASE WHEN b.settingdesc IS NULL THEN b.settingkey WHEN b.settingdesc = '' THEN b.SettingKey WHEN b.settingdesc <> '' THEN b.settingDesc END AS settingdesc,
'SysSettingsDep' AS TableName, B.SettingValue AS settingvalue
FROM SysCustomer AS A INNER JOIN
SysSettingsDep AS B ON A.SettingKey = B.SettingKey
UNION
SELECT C.Id, CASE WHEN c.settingdesc IS NULL THEN c.SettingKey WHEN c.settingdesc = '' THEN c.SettingKey WHEN c.settingdesc <> '' THEN c.settingDesc END AS settingdesc,
'SysSettingsMachine' AS TableName, C.SettingValue AS settingvalue
FROM SysCustomer AS A INNER JOIN
SysSettingsMachine AS C ON A.SettingKey = C.SettingKey
UNION
SELECT D.Id , CASE WHEN d .settingdesc IS NULL THEN d .SettingKey WHEN d .settingdesc = '' THEN d .SettingKey WHEN d .settingdesc <> '' THEN d .settingDesc END AS settingdesc,
'SysSettings' AS TableName, D.SettingValue AS settingvalue
FROM SysCustomer AS A INNER JOIN
SysSettings AS D ON A.SettingKey = d.SettingKey";
SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCommand, MyConnection);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table; dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
这里的问题是当我输入要更新的数字,但字符不更新时.
为什么?
帮助我
The problem here is that when I enter the number to be updated, but the characters do not update.
why?
help me
推荐答案
在下面的代码中,我发现您将要更新设置值.
如果是数字,它将更新,但是如果是字符,则将由于要求使用引号mark('''')
而出现错误.
字符串str = @"update dbo". + tblname +";
str + ="set SettingValue =" +值+";
str + ="where id =''" + id +''";
SqlCommand cm =新的SqlCommand(str,MyConnection);
MyConnection.Open();
SqlDataReader re;
re = cm.ExecuteReader();
re.Close();
MyConnection.Close();
BindGrid();
而不是上面的代码,请尝试以下代码:
字符串str = @"update dbo". + tblname +";
str + ="set SettingValue =''" +值+''";
str + ="where id =''" + id +''";
SqlCommand cm =新的SqlCommand(str,MyConnection);
MyConnection.Open();
SqlDataReader re;
re = cm.ExecuteReader();
re.Close();
MyConnection.Close();
BindGrid();
here in your below code i found that you are going to update setting value .
in case of number it will update but in case of character you will get an error because of it is required quotation mark('''')
string str = @"update dbo." + tblname + "";
str += " set SettingValue = " + value + "";
str += " where id = ''" + id + "''";
SqlCommand cm = new SqlCommand(str, MyConnection);
MyConnection.Open();
SqlDataReader re;
re = cm.ExecuteReader();
re.Close();
MyConnection.Close();
BindGrid();
instead of above code try this one :
string str = @"update dbo." + tblname + "";
str += " set SettingValue = ''" + value + "''";
str += " where id = ''" + id + "''";
SqlCommand cm = new SqlCommand(str, MyConnection);
MyConnection.Open();
SqlDataReader re;
re = cm.ExecuteReader();
re.Close();
MyConnection.Close();
BindGrid();
这篇关于使用SQL查询更新datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文