标题:SQL如何实现更新多笔记录?
只看楼主
StrayInSky
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2007-11-28
 问题点数:0 回复次数:0 
SQL如何实现更新多笔记录?
如何实现更新多条语句?下面代码只适用于所有表的单笔记录更新操作
/// <summary>
        /// 更新表的字段值
        /// </summary>
        /// <param name="Tablename">表名</param>
        /// <param name="keyname">Key数组</param>
        /// <param name="keyvalue">Keyvalue数组</param>
        /// <param name="column">字段名数组</param>
        /// <param name="value">字段名对应值</param>
        /// <returns>成功与否</returns>
        public virtual bool      UpdateTableRow(string Tablename, string[] keyname, string[] keyvalue, string[] column, string[] value)
        {
            if(InteropHelper.HasEmsInterfaces) //如果要求跨平台数据集成
            {
                //这是一个示例,跨平台操作可能不能传递DataTable,那么需要修改接口
                return InteropHelper.UpdateTableRow(Tablename, keyname, keyvalue, column, value);
            }
            #if DEBUG
            try
            {
            #endif
                //没有传入数组或数组对应字段不等
                if (column.Length < 1 || column.Length != value.Length || keyname.Length != keyvalue.Length) return false;
                string sqlString = Tablename + " set ";
                for(int i = 0;i < column.Length; i++ )
                {
                    sqlString = sqlString + column[i] + " = '" + value[i] + "'," ;
                }
                //去掉逗号
                sqlString = sqlString.Substring(0, sqlString.Length - 1);
                if(keyname.Length > 0)
                {
                    sqlString += " where ";
                    for(int j = 0; j < keyname.Length; j++)
                    {
                        sqlString = sqlString + keyname[j] + "= '" + keyvalue[j] + "' and";
                    }
                    //去掉" and"
                    sqlString = sqlString.Substring(0, sqlString.Length - 4);
                }
                sqlConnect.Open();
                SqlCommand updateCmd = new SqlCommand("Update " + sqlString,sqlConnect);
                updateCmd.ExecuteNonQuery();
                sqlConnect.Close();
                return true; //更新成功
            #if DEBUG
            }
            catch(Exception e)
            {
                sqlConnect.Close();
                ProcessLog("Error at UpdateTableRow:" + e);
                return false;  //更新失败
            }
            #endif
        }




要如何实现多笔记录的更新操作呢?
搜索更多相关主题的帖子: SQL 记录 
2008-05-16 11:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-214569-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.315338 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved