using System; using System.Collections.Generic; using System.Text; using System.Data.Odbc; using System.Data.OleDb; namespace ODBCTest { class Program { static void Main(string[] args) { Console.WriteLine("##################################ODBC连接1#############################"); using (OdbcConnection conn = new OdbcConnection("driver={SQL Server};SERVER=127.0.0.1;UID=sa;PWD=Abc123123;DATABASE=booking")) { conn.Open(); OdbcCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from TEST"; using (OdbcDataReader reader = cmd.ExecuteReader()) { Console.WriteLine("字段数:\t{0}", reader.FieldCount); Console.WriteLine("==============数据内容==========="); while (reader.Read()) { Console.WriteLine("ID: {0}\tTitle: {1}", reader.GetInt32(0).ToString(), reader.GetString(1)); } } } Console.WriteLine("################################ODBC连接2###############################"); string conString = "Dsn=ggyy1;" + "Uid=sa;" + "Pwd=Abc123123;";//Dsn表示在ODBC创建的用户数据源名 OdbcConnection con = new OdbcConnection(conString); string sql = "select * from TEST"; //stuinfo为用户数据源绑定的数据库中的一个表 con.Open(); OdbcCommand com = new OdbcCommand(sql, con); using (OdbcDataReader reader = com.ExecuteReader()) { Console.WriteLine("字段数:\t{0}", reader.FieldCount); Console.WriteLine("==============数据内容==========="); while (reader.Read()) { Console.WriteLine("ID: {0}\tTitle: {1}", reader.GetInt32(0).ToString(), reader.GetString(1)); } } Console.WriteLine(); Console.WriteLine("##################################OLEDB连接#############################"); string conString1 = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\test.mdb"; //连接Access数据库 string sql1 = "select count(*) from TEST"; OleDbConnection con1 = new OleDbConnection(conString1); con1.Open(); OleDbCommand com1 = new OleDbCommand(sql1, con1); int i = Convert.ToInt32(com1.ExecuteScalar()); Console.WriteLine(i); Console.Read(); } } }
时间: 2024-10-21 12:30:58