c#查询sql server数据库中的数据时,字段名能否使用变量??
课程设计
1
private void insertfuzhu(string item,string tb)
{
database db = new database();
string insertsql = "insert into fuzhu(patient_id,times,'" + item + "')values('" + patid.Text + "','" + times.Text + "','" + tb + "')')";
string updasql = "update fuzhu set '" + item + "'='" + tb + "' where patient_id='" + patid.Text + "'and times='" + times.Text + "'')";
string sql = "select count(*) from fuzhu where patient_id='" + patid.Text + "'and times='" + times.Text + "'";
int a = 0;
a = db.runsqlint(sql);
if (a > 0)
{
db.runsql(updasql);
}
else
{
db.runsql(insertsql);
}
}
//其中item为表中的字段名,使用变量作为字段名后无法执行查询,新手,不知道怎么办,请大神指教
-
private void insertfuzhu(string item,string tb) { database db = new database(); string insertsql = "insert into fuzhu(patient_id,times," + item + ")values('" + patid.Text + "','" + times.Text + "','" + tb + "')')"; string updasql = "update fuzhu set " + item + "='" + tb + "' where patient_id='" + patid.Text + "'and times='" + times.Text + "'')"; string sql = "select count(*) from fuzhu where patient_id='" + patid.Text + "'and times='" + times.Text + "'"; int a = 0; a = db.runsqlint(sql); if (a > 0) { db.runsql(updasql); } else { db.runsql(insertsql); } } //其中item为表中的字段名,使用变量作为字段名后无法执行查询,新手,不知道怎么办,请大神指教
-
但你定义的变量名称=字段名称
这个字段名称数据库内一定要存在。否则会出错。
字段名不需要+'
发表回复