C#连接DB2

该博客介绍了如何使用C#连接到DB2数据库,通过读取XML配置文件获取连接参数,包括IP地址、端口号、数据库名、用户名、密码等。示例代码展示了建立连接、关闭连接、执行增删改操作及查询的方法。
摘要由CSDN通过智能技术生成

using System;
using System.Xml;
using System.Collections;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data;

namespace testmap2
{
 /// <summary>
 /// DBAccess_DB2 的摘要说明。
 /// </summary>
 public class DBAccess_DB2
 {
  private string location;      //IP地址
  private string data_source;   //数据库名字
  private int appsvr;           //端口号
  private string Provider;     
  private string user_id;       //用户名
  private string password;      //密码
  private bool persist_Security_Info;
  private OleDbConnection con;
  public DBAccess_DB2(string filePath)
  {
   //filePath=System.AppDomain.CurrentDomain.BaseDirectory+"LegendNetCommonOracle.xml"
   this.CheckConfig(filePath);
  }
#region 读XML中的DB2配置信息
  private void CheckConfig(string filePath)
  {
   try
   {
    XmlDocument _xd=new XmlDocument();
    _xd.Load(filePath);
    XmlElement root=_xd.DocumentElement;
    XmlNodeList _xnl=root.GetElementsByTagName("db_cfgDB2");
    IEnumerator ienum = _xnl.GetEnumerator();
    ienum.MoveNext();
    ienum=((XmlNode)ienum.Current).ChildNodes.GetEnumerator();
    while(ienum.MoveNext())
    { 
     XmlNode title = (XmlNode) ienum.Current;
     switch(title.Name)
     {
      case "Location":
      {
       this.location=title.InnerText;
       break;
      }
      case "data_source":
      {
       this.data_source=title.InnerText;
       break;
      }
      case "appsvr":
      {
       this.appsvr=int.Parse(title.InnerText);
       break;
      }
      case "Provider":
      {
       this.Provider=title.InnerText;
       break;
      }
      case "user_id":
      {
       this.user_id=title.InnerText;
       break;
      }
      case "password":
      {
       this.password=title.InnerText;
       break;
      }
      case "Persist_Security_Info":
      {
       this.persist_Security_Info=bool.Parse(title.InnerText);
       break;
      }
     }
    }
   }
   catch(Exception e)
   {
    throw new Exception(e.Message);
   }
  }
#endregion 
#region 获得连接
  public void getcon()/Provider=IBMDADB2.1  appsvr:50000;
  {
   try
   {
    string constring="Location="+this.location+";Provider=/""+this.Provider+"/";User ID="+this.user_id+";Data Source="+this.data_source+";Perist Security Info="+this.persist_Security_Info+";Password="+this.password+";appsvr="+this.appsvr;
    con=new OleDbConnection(constring);
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }

   
  }
#endregion
#region 关闭连接
  public void closecon()
  {
   if(con.State==ConnectionState.Open)
    con.Close();
  }
#endregion
#region 用于增删改操作
  public int execute(string sql)
  {
   int i=0;
   try
   {
    this.getcon();
    OleDbCommand cmd=new OleDbCommand(sql,con);
    con.Open();
    i=cmd.ExecuteNonQuery();
    this.closecon();
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
   return i;
  }
#endregion
#region 查询方法
  public DataTable search(string sql)
  {
   DataSet ds=null;
   try
   {
    this.getcon();
    ds=new DataSet();
    OleDbDataAdapter da=new OleDbDataAdapter(sql,con);
    da.Fill(ds);
    this.closecon();
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
   return ds.Tables[0];
  }
#endregion

#region 检验能否获得XML文件中的内容
  public void xianshi()
  {
   MessageBox.Show(this.location+"/r/n"+this.appsvr+"/r/n"+this.user_id+"/r/n"+this.password+"/r/n"+this.Provider+"/r/n"+this.persist_Security_Info+"/r/n"+this.data_source);
  }
#endregion
 }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值