首页 新闻 赞助 找找看

SqlParameter参数 怎么赋值

0
悬赏园豆:5 [已关闭问题] 关闭于 2014-04-10 14:40

RT,假如有这样一条语句:

UPDATE Tab_Operation SET isSync=@isSync where Id in (@Id)

isSync的值不用管,主要是Id这个值该怎么赋值呢?如果Id是string类型的,又该如何解决?

示例代码附上

 1   public static int UpdateSync(string tableName, string codeName, List<string> codeList, int isSync)
 2         {
 3             StringBuilder strSql = new StringBuilder();
 4             int result = 0;
 5             strSql.Append("UPDATE ");
 6             strSql.Append(tableName);
 7             strSql.Append(" SET ");
 8             strSql.Append(" isSync = @isSync WHERE ");
 9             strSql.Append(codeName);
10             strSql.Append(" in (@code)");
11             if (codeList.Count > 0)
12             {
13                 SqlParameter[] parameters =
14                 {
15                     new SqlParameter("@code", string.Join(",", codeList.ToArray())),
16                     new SqlParameter("@isSync", isSync)
17                 };
18                 try
19                 {
20                     result = SqlHelper.ExecuteNonQuery(CenterConnectionString, CommandType.Text, strSql.ToString(), null);
21                 }
22                 catch (Exception ex)
23                 {
24                     MessageBox.Show(ex.Message);
25                     WriteLogs("执行的SQL语句是:" + strSql);
26                 }
27             }
28 
29             return result;
30         }    
sam.c的主页 sam.c | 初学一级 | 园豆:148
提问于:2013-12-26 17:35
< >
分享
所有回答(5)
0

在sqlparm的value里面,'1','2','3','4' 拼接成这样的格式

Plusone | 园豆:344 (菜鸟二级) | 2013-12-26 17:46
0

in()里面如果用参数化的话,只会当成一个参数处理,达不到in的效果。

如 ids="1,2,3"      in(@ids)    到数据库为'1,2,3'

幻天芒 | 园豆:37175 (高人七级) | 2013-12-26 19:14
0

支持二楼 用参数的话,只会当一个值

把后面的in () 里面的拼成字符串

Albert Fei | 园豆:2102 (老鸟四级) | 2013-12-27 09:39
0

in这样用的话达不到效果,或者是拼成sql字符串 动态执行,要不就考虑下like 跟 charIndex 函数

havid | 园豆:70 (初学一级) | 2013-12-30 17:30
0

可以考虑使用拼接SQL字符串,where 条件使用=, Id 可以是string的 ,这个思路简单,不宜出错

public static int UpdateSync(string tableName, string codeName, List<string> codeList, int isSync)
        {
            int result = 0;
            var strSql = "UPDATE @tableName  SET isSync=@isSync where @param =@paramVal";
            StringBuilder strSqls = new StringBuilder();
            for (int i = 0; i < codeList.Count; i++)
            {
                var sql = strSql;
                sql = sql.Replace("@tableName", tableName)
                         .Replace("@isSync", isSync.ToString())
                         .Replace("@param", codeList[i]);
                strSqls.AppendLine(sql);
            }
            try
            {
                result = SqlHelper.ExecuteNonQuery(CenterConnectionString, CommandType.Text, strSqls.ToString(), null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                WriteLogs("执行的SQL语句是:" + strSql);
            }
            return result;
        }
shzy2012 | 园豆:224 (菜鸟二级) | 2014-03-04 14:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册