weixin_44151228 2021-02-09 23:11 采纳率: 58.3%
浏览 202
已结题

c#查询sql server数据库中的数据时,字段名能否使用变量??

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为表中的字段名,使用变量作为字段名后无法执行查询,新手,不知道怎么办,请大神指教
  • 写回答

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为表中的字段名,使用变量作为字段名后无法执行查询,新手,不知道怎么办,请大神指教
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月31日

悬赏问题

  • ¥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语言代码为何输出了多余的空格