达梦数据库管理系统是达梦公司推出的具有完全自主知识产权的高性能数据库管理系统,简称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