使用数组创建字符串

编程入门 行业动态 更新时间:2024-10-27 06:32:02
本文介绍了使用数组创建字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用数组创建字符串, 例如:更新表集名称= @名称,地址= @ 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.

更多推荐

使用数组创建字符串

本文发布于:2023-10-11 13:53:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!