C#连接达梦数据库
达梦数据库提供了很多驱动,包括.Net 。在达梦数据的安装目录找到 \dmdbms\bin\ DmProvider.dll 程序集,这就是.Net 操作达梦数据库的驱动程序。我们用VS建个Demo 引用这个dll。
增删改查的演示代码如下:
using Dm;
/// <summary>
/// 获得数据库链接
/// </summary>
/// <returns></returns>
public DmConnection GetConn()
{
string strconn = "server=localhost;database=Cnblogs;User Id=SYSDBA;PWD=SYSDBA";
DmConnection conn = new DmConnection(strconn);
conn.Open();
return conn;
}
//插入数据
public void Insert()
{
DmConnection conn = GetConn();
string strCmd = "Insert into SYSDBA.Users(Name,Sex,Age)values('xumingxiang','man',25)";
DmCommand cmd = new DmCommand(strCmd, conn);
int effect = cmd.ExecuteNonQuery();
conn.Close();//关闭数据库链接
}
/// <summary>
/// 删除数据
/// </summary>
public void Update()
{
DmConnection conn = GetConn();
string strCmd = "update SYSDBA.Users set Age=100 where Id=1";
DmCommand cmd = new DmCommand(strCmd, conn);
int effect = cmd.ExecuteNonQuery();
conn.Close();
}
/// <summary>
/// 删除数据
/// </summary>
public void Delete()
{
DmConnection conn = GetConn();
string strCmd = "delete from SYSDBA.Users where Id=1";
DmCommand cmd = new DmCommand(strCmd, conn);
int effect = cmd.ExecuteNonQuery();
conn.Close();
}
/// <summary>
/// 用ExecuteReader查询数据
/// </summary>
public void QueryByExecuteReader()
{
DmConnection conn = GetConn();
string strCmd = "select * from SYSDBA.Users ";
DmCommand cmd = new DmCommand(strCmd, conn);
DmDataReader dr = cmd.ExecuteReader();
int id;
string name;
string sex;
int age;
while (dr.Read())
{
id = dr.GetInt32(0);
name = dr.GetString(1);
sex = dr.GetString(2);
age = dr.GetInt32(3);
}
}
/// <summary>
///用DataAdapter 查询数据,返回DataSet
/// </summary>
public DataSet QueryByDataAdapter()
{
DmConnection conn = GetConn();
string strCmd = "select * from SYSDBA.Users ";
DmDataAdapter da = new DmDataAdapter(strCmd,conn);
DataSet ds =