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
}
}