NET(C#)連線各類資料庫-集錦 --【C#網文收集系列】.
1.C#連線連線Access
程式程式碼:
-------------------------------------------------------------------------------
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
--------------------------------------------------------------------------------
解釋:
連線Access資料庫需要匯入額外的名稱空間,所以有了最前面的兩條using命令,這是必不可少的!
strConnection這個變數裡存放的是連線資料庫所需要的連線字串,他指定了要使用的資料提供者和要使用的資料來源.
"Provider=Microsoft.Jet.OleDb.4.0;"是指資料提供者,這裡使用的是Microsoft Jet引擎,也就是Access中的資料引擎,asp.net就是靠這個和Access的資料庫連線的.
"Data Source=C:/BegASPNET/Northwind.mdb"是指明資料來源的位置,他的標準形式是"Data Source=MyDrive:MyPath/MyFile.MDB".
PS:
1."+="後面的"@"符號是防止將後面字串中的"/"解析為轉義字元.
2.如果要連線的資料庫檔案和當前檔案在同一個目錄下,還可以使用如下的方法連線:
strConnection+="Data Source=";
strConnection+=MapPath("Northwind.mdb");
這樣就可以省得你寫一大堆東西了!
3.要注意連線字串中的引數之間要用分號來分隔.
"OleDbConnection objConnection=new OleDbConnection(strConnection);"這一句是利用定義好的連線字串來建立了一個連結物件,以後對資料庫的操作我們都要和這個物件打交道.
"objConnection.Open();"這用來開啟連線.至此,與Access資料庫的連線完成.
--------------------------------------------------------------------------------
2.C#連線SQL Server
程式程式碼:
--------------------------------------------------------------------------------
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
--------------------------------------------------------------------------------
解釋:
連線SQL Server資料庫的機制與連線Access的機制沒有什麼太大的區別,只是改變了Connection物件和連線字串中的不同引數.
首先,連線SQL Server使用的名稱空間不是"System.Data.OleDb",而是"System.Data.SqlClient".
其次就是他的連線字串了,我們一個一個引數來介紹(注意:引數間用分號分隔):
"user id=sa":連線資料庫的驗證使用者名稱為sa.他還有一個別名"uid",所以這句我們還可以寫成"uid=sa".
"password=":連線資料庫的驗證密碼為空.他的別名為"pwd",所以我們可以寫為"pwd=".
這裡注意,你的SQL Server必須已經設定了需要使用者名稱和密碼來登入,否則不能用這樣的方式來登入.如果你的SQL Server設定為Windows登入,那麼在這裡就不需要使用"user id"和"password"這樣的方式來登入,而需要使用"Trusted_Connection=SSPI"來進行登入.
"initial catalog=Northwind":使用的資料來源為"Northwind"這個資料庫.他的別名為"Database",本句可以寫成"Database=Northwind".
"Server=YourSQLServer":使用名為"YourSQLServer"的伺服器.他的別名為"Data Source","Address","Addr".如果使用的是本地資料庫且定義了例項名,則可以寫為"Server=(local)/例項名";如果是遠端伺服器,則將"(local)"替換為遠端伺服器的名稱或IP地址.
"Connect Timeout=30":連線超時時間為30秒.
在這裡,建立連線物件用的建構函式為:SqlConnection.
--------------------------------------------------------------------------------
3.C#連線Oracle
程式程式碼:
--------------------------------------------------------------------------------
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
-------------------------------------------------------------------------------
4.C#連線MySQL
程式程式碼:
--------------------------------------------------------------------------------
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.putfly.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
--------------------------------------------------------------------------------
5.C#連線IBM DB2
程式程式碼:
--------------------------------------------------------------------------------
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
![](http://www.putfly.com/Images/OutliningIndicators/None.gif)
--------------------------------------------------------------------------------
6.C#連線SyBase
程式程式碼: (OleDb)
--------------------------------------------------------------------------------
Provider=Sybase.ASEOLEDBProvider.2;Initial Catalog=資料庫名;User ID=使用者名稱;Data Source=資料來源;Extended Properties="";Server Name=ip地址;Network Protocol=Winsock;Server Port Address=5000;