使用数组创建字符串 [英] Create string using array

查看:121
本文介绍了使用数组创建字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用数组创建字符串,
例如:更新表集名称= @名称,地址= @ addr,年龄= @年龄等.
我想使用数组创建此命令,因为每次更新命令对于不同的表都是不同的.所以我只想写一次命令.

请任何人在这方面帮助我.

在Advance中致谢

I want to create string using array,
Ex: update table set name=@name,address=@addr,age=@age etc.
I want to create this command using array because every time the update command is different for different tables. So i want to write command only one time.

Please any one help me in this regard.

Thanks in Advance

推荐答案

使用字典将此放置列名称作为键以及相应的值-
use Dictionary for this put column name as key and the corresponding value-
void Runcommand(string tableName,Dictionary<string,object>updatevalues, string wherecaluse)
{
  SqlCommand cmd=new SqlCommand(conn);
  cmd.CommandText="update "+tableName +" Set ";
  bool isAdded=false;
  foreach(string key in updatevalues.keys)
  {
    if(isAdded)
    {
      cmd.CommandText+=", ";
    }
    isAdded=true;
    cmd.CommandText+=string.Format("{0}=@{0}",
    cmd.Parameters.Add(new SqlParameter(key,updatevalues[key]);
  }
  cmd.CommandText+=" where "+whereclause;
  cmd.ExcuteNonQuery();
  
}



希望这对您有用.



hope this will work for you.


这篇关于使用数组创建字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆