C#连接db2数据库
1、通过OLE DB for DB2驱动
string strSql = @"select phone_no from no_store where id<5"; string strConn = "Provider=IBMDADB2;Data Source=数据库名;UID=用户名;PWD=密码;"; using (OleDbConnection conn = new OleDbConnection(strConn)) { OleDbCommand cmd = new OleDbCommand(strSql, conn); try { conn.Open(); OleDbDataAdapter adp = new OleDbDataAdapter(cmd); DataSet ds = new DataSet(); adp.Fill(ds); DataTable dt = ds.Tables[0]; if (dt != null) { for (int i = 0; i < dt.Rows.Count; i++) { Console.WriteLine("电话" + i + ":" + dt.Rows[i][0].ToString()); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } Console.Read();
2、通过IBM提供的IBM.data.DB2.DLL
string strSql = @"select phone_no from no_store where id<5"; string strConn = "Database=数据库名;UID=用户名;PWD=密码;"; using (DB2Connection conn = new DB2Connection(strConn)) { DB2Command cmd = new DB2Command(strSql, conn); try { conn.Open(); DB2DataAdapter adp = new DB2DataAdapter(cmd); DataSet ds = new DataSet(); adp.Fill(ds); DataTable dt = ds.Tables[0]; if (dt != null) { for (int i = 0; i < dt.Rows.Count; i++) { Console.WriteLine("电话" + i + ":" + dt.Rows[i][0].ToString()); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } Console.Read();