C#连接国产化达梦数据库

达梦数据库管理系统是达梦公司推出的具有完全自主知识产权的高性能数据库管理系统,简称DM。 

达梦数据库试用版下载地址:产品下载-达梦数据

 C#操作DM数据库封装

   public  class DMDbHelper
    {
        private static string dbConnectionString = "User Id=SYSDBA;Password=ASDFGHasdfgh123456;Host=LOCALHOST;Port=5236;Database=WMSDB";
        private static string dbProviderName = "Dm.Data.DmClient";
        private DmConnection connection;

        public DMDbHelper()
        {
            connection = new DmConnection(dbConnectionString);
        }

        public DMDbHelper(string connectionString)
        {
            this.connection = new DmConnection(connectionString);
        }
        public DmCommand  GetStoredProcCommond(string storedProcedure)
        {
            DmCommand DmCommand = new DmCommand(storedProcedure,connection);//connection.CreateCommand();
            DmCommand.CommandText = storedProcedure;
            DmCommand .CommandType = CommandType.StoredProcedure;
            return DmCommand ;
        }

        public DmCommand  GetSqlStringCommond(string sqlQuery)
        {
            DmCommand  DmCommand  = new DmCommand(sqlQuery, connection);//connection.CreateCommand();
            DmCommand .CommandText = sqlQuery;
            DmCommand .CommandType = CommandType.Text;
            return DmCommand ;
        }
        #region 增加参数

        public void AddParameterCollection(DmCommand  cmd, DbParameterCollection dbParameterCollection)
        {
            foreach (DbParameter dbParameter in dbParameterCollection)
            {
                cmd.Parameters.Add(dbParameter);
            }
        }

        public void AddOutParameter(DmCommand cmd, string parameterName, DbType dbType, int size)
        {
            //DbParameter dbParameter = cmd.CreateParameter();
            DmParameter dbParameter = new DmParameter();
            dbParameter.DbType = dbType;
            dbParameter.ParameterName = parameterName;
            dbParameter.Size = size;
            dbParameter.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(dbParameter);
        }
        public void AddOutParameter(DmCommand  cmd, string parameterName, DbType dbType, int size, object value)
        {
            //DbParameter dbParameter = cmd.CreateParameter();
            DmParameter dbParameter = new DmParameter();
            dbParameter.DbType = dbType;
            dbParameter.ParameterName = parameterName;
            dbParameter.Size = size;
            dbParameter.Value = value;
            dbParameter.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(dbParameter);
        }
        public void AddInParameter(DmCommand  cmd, string parameterName, DbType dbType, object value)
        {
            DbParameter dbParameter = cmd.CreateParameter();
            //DbParameter dbParameter = cmd.CreateDbParameter();
            dbParameter.DbType = dbType;
            dbParameter.ParameterName = parameterName;
            dbParameter.Value = value;
            dbParameter.Direction = ParameterDirection.Input;
            // 创建一个DmParameter对象,并设置其ParameterName和Value属性  
            cmd.Parameters.Add(dbParameter);
        }

        public void AddReturnParameter(DmCommand  cmd, string parameterName, DbType dbType)
        {
            //DbParameter dbParameter = cmd.CreateParameter();
            DmParameter dbParameter = new DmParameter();
            dbParameter.DbType = dbType;
            dbParameter.ParameterName = parameterName;
            dbParameter.Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(dbParameter);
        }

        public DbParameter GetParameter(DmCommand  cmd, string parameterName)
        {
            return cmd.Parameters[parameterName];
        }

        #endregion 增加参数

        #region 执行

        public DataSet ExecuteDataSet(DmCommand  cmd)
        {
            try
            {
                if (cmd.Connection.State == ConnectionState.Closed)
                {
                    cmd.Connection.Open();
                }
                DataSet ds = new DataSet();
                using (DbDataReader reader = cmd.ExecuteReader())
                {
                    DataTable dataTable = new DataTable();
                    dataTable.Load(reader);
                    ds.Tables.Add(dataTable); // 如果你有多个表,就重复这个过程
                    dataTable.Dispose();
                }
               
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (cmd.Connection.State == ConnectionState.Open)
                {
                    cmd.Connection.Close();
                }
            }
        }

        public DataTable ExecuteDataTable(DmCommand  cmd)
        {
            try
            {
                if (cmd.Connection.State == ConnectionState.Closed)
                {
                    cmd.Connection.Open();
                }
                DataTable dataTable = new DataTable();
                // 模拟使用DbDataReader填充DataTable  
                using (DbDataReader reader = cmd.ExecuteReader())
                {
                    dataTable.Load(reader);
                }
                return dataTable;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (cmd.Connection.State == ConnectionState.Open)
                {
                    cmd.Connection.Close();
                }
            }
        }

        public DbDataReader ExecuteReader(DmCommand  cmd)
        {
            try
            {
                if (cmd.Connection.State == ConnectionState.Closed)
                {
                    cmd.Connection.Open();
                }
                DbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return reader;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (cmd.Connection.State == ConnectionState.Open)
                {
                    cmd.Connection.Close();
                }
            }
        }

        public int ExecuteNonQuery(DmCommand  cmd)
        {
            try
            {
                if (cmd.Connection.State == ConnectionState.Closed)
                {
                    cmd.Connection.Open();
                }
                int ret = cmd.ExecuteNonQuery();

                return ret;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (cmd.Connection.State == ConnectionState.Open)
                {
                    cmd.Connection.Close();
                }
            }
        }

        public object ExecuteScalar(DmCommand  cmd)
        {
            try
            {
                if (cmd.Connection.State == ConnectionState.Closed)
                {
                    cmd.Connection.Open();
                }
                object ret = cmd.ExecuteScalar();
                if (cmd.Connection.State == ConnectionState.Open)
                {
                    cmd.Connection.Close();
                }
                return ret;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (cmd.Connection.State == ConnectionState.Open)
                {
                    cmd.Connection.Close();
                }
            }
        }

        #endregion 执行

    }

需要导入DmProvider.dll

调用Demo

DMDbHelper dbHelper = new DMDbHelper();

            try
            {
                DmCommand cmd = dbHelper.GetSqlStringCommond(@"DELETE FROM WMSDB.TABLE_1");
                cmd.CommandTimeout = 10000;
                dbHelper.ExecuteNonQuery(cmd);
            }
            catch
            {

            }

            try
            {
                DmCommand cmd = dbHelper.GetSqlStringCommond(@"INSERT INTO WMSDB.TABLE_1(COLUMN_2)VALUES('"+ DateTime.Now.ToString("yyMMddHHmmssfff") + "') ; ");
                cmd.CommandTimeout = 10000;
                dbHelper.ExecuteNonQuery(cmd);
            }
            catch
            {

            }

            try
            {
                DmCommand cmd = dbHelper.GetSqlStringCommond("INSERT INTO WMSDB.TABLE_1(COLUMN_2)VALUES(:COLUMN_2) ; ");
                cmd.CommandTimeout = 10000;
                dbHelper.AddInParameter(cmd, ":COLUMN_2", DbType.String, DateTime.Now.ToString("yyMMddHHmmssfff"));
                dbHelper.ExecuteNonQuery(cmd);
            }
            catch
            {

            }

            try
            {
                DmCommand cmd = dbHelper.GetSqlStringCommond(@"select COLUMN_1,COLUMN_2 from WMSDB.TABLE_1; ");
                cmd.CommandTimeout = 10000;
                DataTable data = dbHelper.ExecuteDataTable(cmd);
                if (data != null)
                {

                }
            }
            catch
            {

            }

            #region //存储过程

            try
            {
                DmCommand cmd = dbHelper.GetStoredProcCommond(@"WMSDB.CE");
                cmd.CommandTimeout = 10000;
                dbHelper.AddInParameter(cmd, ":NAME", DbType.String, DateTime.Now.ToString("yyMMddHHmmssfff"));
                dbHelper.AddOutParameter(cmd, ":HH", DbType.String,1000);
                dbHelper.ExecuteNonQuery(cmd);
                string HH =dbHelper.GetParameter(cmd,":HH").Value.ToString();
                MessageBox.Show(HH);
            }
            catch
            {

            }

            #endregion

C#连接达梦(DM)数据库Demo下载

C#连接人大金仓数据库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值