原文: C#中的ODBC、OLEDB連接
?
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( " 字段數(shù):\t{0} " , reader.FieldCount); Console.WriteLine( " ==============數(shù)據(jù)內(nèi)容=========== " ); 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創(chuàng)建的用戶數(shù)據(jù)源名 OdbcConnection con = new OdbcConnection(conString); string sql = " select * from TEST " ; // stuinfo為用戶數(shù)據(jù)源綁定的數(shù)據(jù)庫中的一個表 con.Open(); OdbcCommand com = new OdbcCommand(sql, con); using (OdbcDataReader reader = com.ExecuteReader()) { Console.WriteLine( " 字段數(shù):\t{0} " , reader.FieldCount); Console.WriteLine( " ==============數(shù)據(jù)內(nèi)容=========== " ); 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數(shù)據(jù)庫 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(); } } }
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
