DataGridView数据更新至数据库-逐条更新

简介:

首先判断是否存在指定记录,存在则执行更新语句,不存在则执行插入语句。主要用到三个函数:

 public class PubVariant
    {
        public static string strUpdateSql = "update CorrespondFields set CadField = @CadField,FieldType = @FieldType,CADTYPE = @CADTYPE"
            + " where SdeLayerName = @SdeLayerName and CadLayerName = @CadLayerName and SdeField = @SdeField";
        public static string strInsertSql = "insert into CorrespondFields values(@SdeLayerName,@CadLayerName,@SdeField,@CadField,@FieldType,@CADTYPE)";
    }

        /// <summary>
        /// 判断数据库是否有指定键值的记录
        /// </summary>
        /// <param name="str">键值</param>
        /// <returns>是否存在记录的布尔值</returns>
        public static bool ExistsRecord(string str)
        {
            string strSql = "select * from CorrespondFields where SdeField = '" + str + "' and SdeLayerName = '" + PubVariant.sdeLayerName
            + "' and CadLayerName = '" + PubVariant.cadLayerName + "'";
            using (SqlConnection connection = new SqlConnection(PubVariant.ConnectionString))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandText = strSql;
                cmd.CommandType = CommandType.Text;
                SqlDataReader datareader = cmd.ExecuteReader();
                return datareader.HasRows;
            }
            
        }

        /// <summary>
        /// 执行带参数的Sql语句
        /// </summary>
        /// <param name="sqlParas">sql参数数组</param>
        /// <param name="strSql">要执行的sql语句</param>
        public static void ExecuteSql(SqlParameter[] sqlParas, string strSql)
        {
            using (SqlConnection connection = new SqlConnection(PubVariant.ConnectionString))
            {
                connection.Open();
                using (SqlCommand cmd = new SqlCommand(strSql, connection))
                {
                    foreach (SqlParameter sp in sqlParas)
                    {
                        cmd.Parameters.Add(sp);
                    }
                    cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// 用datagridview的数据更新数据库
        /// </summary>
        /// <param name="dgv">datagridview</param>
        /// <returns>更新是否成功</returns>
        public static bool UpdataFromDGVtoDB(DataGridView dgv)
        {
            try
            {
                for (int i = 0; i < dgv.Rows.Count - 1; i++)
                {
                    string strCADTYPE;
                    if (dgv.Rows[i].Cells[1].Value.ToString().StartsWith("["))
                    {
                        strCADTYPE = "1";
                    }
                    else
                    {
                        strCADTYPE = "2";
                    }

                    SqlParameter[] sqlParas = new SqlParameter[]
                    {
                        new SqlParameter("@SdeLayerName", PubVariant.sdeLayerName),
                        new SqlParameter("@CadLayerName", PubVariant.cadLayerName),
                        new SqlParameter("@SdeField", dgv.Rows[i].Cells[0].Value.ToString()),
                        new SqlParameter("@CadField", dgv.Rows[i].Cells[1].Value.ToString()),
                        new SqlParameter("@FieldType", dgv.Rows[i].Cells[2].Value.ToString()),
                        new SqlParameter("@CADTYPE", strCADTYPE)                        
                    };

                    if (ExistsRecord(dgv.Rows[i].Cells[0].Value.ToString()))
                    {
                        ExecuteSql(sqlParas, PubVariant.strUpdateSql);
                    }
                    else
                    {
                        ExecuteSql(sqlParas, PubVariant.strInsertSql);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示");
                return false;
            }
        }


转载:http://blog.csdn.net/foreverling/article/details/36390709

目录
相关文章
|
2月前
|
SQL 算法 数据库
【数据库SQL server】关系数据库标准语言SQL之数据更新
【数据库SQL server】关系数据库标准语言SQL之数据更新
33 0
|
3月前
|
SQL 关系型数据库 MySQL
『 MySQL数据库 』CRUD之UD,表的数据更新(修改)及删除
『 MySQL数据库 』CRUD之UD,表的数据更新(修改)及删除
|
9月前
|
SQL 数据库
数据库上机实验4 数据更新和视图
数据库上机实验4 数据更新和视图
83 0
|
4月前
|
SQL 数据库连接 数据库
C# | 将DataGridView中的数据保存到Accesss数据库
要将WinForm的DataGridView中的数据保存到Access数据库,可以按照本文的步骤进行。 在Visual Studio中,打开项目,右键单击“引用”文件夹,选择“添加引用”,在“COM”选项卡中找到并选中“Microsoft Office 14.0 Access Database Engine Object Library”,然后单击“确定”按钮。
91 0
C# | 将DataGridView中的数据保存到Accesss数据库
|
7月前
|
SQL 数据库 数据库管理
第3章 关系数据库标准语言SQL——3.5 数据更新
第3章 关系数据库标准语言SQL——3.5 数据更新
|
SQL 数据库
数据库实验三——数据更新操作中经典题、难题以及易错题合集(含数据导入导出操作详细教程)
数据库实验三——数据更新操作中经典题、难题以及易错题合集(含数据导入导出操作详细教程)
156 0
数据库实验三——数据更新操作中经典题、难题以及易错题合集(含数据导入导出操作详细教程)
|
SQL 数据管理 关系型数据库
数据库原理及应用——数据更新和视图创建
(1)所使用的学生管理库中的三张表: (2)数据更新具体完成以下例题: (3)视图操作具体完成以下例题:
502 0
数据库原理及应用——数据更新和视图创建
|
C# 数据库
C#编程-75:DataGridView直接修改数据库_
C#编程-75:DataGridView直接修改数据库_
241 0
|
SQL Oracle 关系型数据库
Oracle 数据库 根据B表的数据更新A表
Oracle 数据库 根据B表的数据更新A表
714 0
|
SQL 数据库
VB:使用Visual Studio 2010中的VB语言工具箱DataGridView调用SQL数据库Database的表格文件
VB:使用Visual Studio 2010中的VB语言工具箱DataGridView调用SQL数据库Database的表格文件
VB:使用Visual Studio 2010中的VB语言工具箱DataGridView调用SQL数据库Database的表格文件

热门文章

最新文章