首页 > 数据库 > 数据库开发技术 > Visual C#中实现DB2数据库编程(转)
在Visual Studio.NET Beta 1版本中访问IBM DB2等非SQL Server数据库通常是使用ADO.NET的ODBC方法,而在Beta 2中则改用OLE DB方式存取数据库。
Beta1中连接字符串主要需声明数据库的ODBC的DNS名字,但Beta 2中的OLE DB连接的字符串就较复杂了,使用的类也不相同。由于DB2等数据库在大型机等应用中使用非常广泛,.NET自然会全力支持此类编程开发,为此,下文将逐步阐述OLE DB的DB2开发。
首先,Visual Studio.NET和Windows 2000 Server中并未直接提供DB2的OLE DB驱动,因此需下载Microsoft Host Integration Server 2000软件,该软件包中含OLE DB Provider and ODBC driver for DB2,需下载Server端软件包,并在安装时选择安装Data Integration,安装完成后就可使用其OLE DB的DB2驱动了。
首先,我们需配置好DB2的客户端设置,打开视图选单项中的服务器资源管理器,在其数据连接中新增一个连接,指定程序选项时选择Microsoft OLE DB Provider for DB2,下一步连接选项中,填写如下信息:
| Data Source: QCDB(请酌情修改) Network:TCP/IP IP Address:数据库服务器地址 Network Port:DB2服务端口 Username:数据库访问权限的用户名 Password:该用户口令 Database initial Catalog:QCDB Package collection:QCDB default schema:Userid |
| this.oleDbConnection1.ConnectionString = @"Provider=DB2OLEDB; Cache Authentication=False; Integrated Security=""""; Password=freebsd9; Persist Security Info=True; User ID=userid;Initial Catalog=qcdb; Data Source=qcdb;Mode=ReadWrite; Extended Properties=""""; APPC Remote LU Alias=""""; APPC Local LU Alias=""""; APPC Mode Name=QPCSUPP; Network Transport Library=TCPIP; Host CCSID=37;PC Code Page=1282; Network Address=172.23.169.1; Network Port=4444;Package Collection=qcdb; Default Schema=userid;Alternate TP Name=""""; Process Binary as Character=False; Units of Work=RUW" |
| oleDbConnection1.Open(); //打开数据库连接 oleDbDataAdapter1.Fill(dataSet1,"Address"); //将得来的数据填入dataSet DataGrid1.DataBind(); //绑定数据 oleDbConnection1.Close(); //关闭连接 |
| this.oleDbInsertCommand1.CommandText = "INSERT INTO ADDRESS(NAME, EMAIL, AGE, ADDRESS) VALUES ('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')"; oleDbInsertCommand1.Connection.Open(); //打开连接 oleDbInsertCommand1.ExecuteNonQuery(); //执行该SQL语句 oleDbInsertCommand1.Connection.Close(); //关闭连接 |
| System.Data.OleDb.OleDbCommand oleDeleteCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbDataAdapter1.DeleteCommand = oleDeleteCommand1; //声明为oleDb命令 oleDeleteCommand1.CommandText="DELETE FROM ADDRESS WHERE NAME='"+TextBox5.Text+"'"; oleDeleteCommand1.Connection = this.oleDbConnection1; //指明连接 oleDeleteCommand1.Connection.Open(); //打开连接 oleDeleteCommand1.ExecuteNonQuery(); //执行SQL语句 oleDeleteCommand1.Connection.Close(); //关闭连接 |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8781179/viewspace-924599/ ,如需转载,请注明出处,否则将追究法律责任。