首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

SQL2005 批量更新有关问题,

2012-07-05 
SQL2005 批量更新问题,急。update userinfo username ‘a’ where id 1update userinfo username ‘b’ w

SQL2005 批量更新问题,急。
update userinfo username = ‘a’ where id = 1
update userinfo username = ‘b’ where id = 2
update userinfo username = ‘c’ where id = 3
update userinfo username = ‘d’ where id = 4
update userinfo username = ‘e’ where id = 5

能写成一条吗??批量更新了
ID 知道用in




[解决办法]

C# code
update userinfo set username = case                                  when id = 1 then 'a'                                  when id = 2 then 'b'                                  when id = 3 then 'c'                                  when id = 4 then 'd'                                  when id = 5 then 'e'                               end
[解决办法]
C# code
        StringBuilder sb = new StringBuilder();        for (int i = 1; i <= 5; i++)            sb.AppendFormat(@"update userinfo username='{0}' where id={1};", ((char)(i + 0x60)).ToString(), i);        string sqlStr = sb.ToString();
[解决办法]
直接执行:update userinfo set username=char(ascii(id)+0x30) where id in(1,2,3,4,5)
试过可以。
[解决办法]
两个字符串的话你可以是一下下嘛的例子:
C# code
 string s1= "a,b,c,d,e,f,g";//s1,s2 是你的两个字符串         string[] s1_array = svalue.Split(',');         string s2= "1,2,3,4,5,6,7";         string[] s2_array = svalue.Split(',');        for(int i=0;i<s1_array.Length;i++)//s1_array和s2_array长度肯定是一样的所以用那个都行           {             “update userinfo username='”+s1_array[i]+"'where id="+s2_array[i]+"";//执行你的sql语句           } 

热点排行