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为表中的字段名,使用变量作为字段名后无法执行查询,新手,不知道怎么办,请大神指教
c#查询sql server数据库中的数据时,字段名能否使用变量??
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- 歇歇 2021-02-09 23:27关注
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为表中的字段名,使用变量作为字段名后无法执行查询,新手,不知道怎么办,请大神指教
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥30 Matlab打开默认名称带有/的光谱数据
- ¥50 easyExcel模板 动态单元格合并列
- ¥15 res.rows如何取值使用
- ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
- ¥15 CSP算法实现EEG特征提取,哪一步错了?
- ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
- ¥15 vue3前端取消收藏的不会引用collectId
- ¥15 delphi7 HMAC_SHA256方式加密
- ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
- ¥15 下列c语言代码为何输出了多余的空格