標籤:style blog ar io color os 使用 sp on
- Connection:用於串連資料來源
- Command:對資料來源執行命令
- DataReader:在唯讀和唯寫的串連模式下從資料來源讀取資料.
- DataAdpter:從資料來源讀取資料並使用所讀取的資料填充資料集對象.
建立Connection
1,用SqlConnection串連Sql Server
(1)引入命名空間:
using System.Data.SqlClient;
(2)串連資料庫:
string conString="data source=127.0.0.1;Database=codematic;user id=sa;password=";SqlConnection myConnection=new SqlConnection(conString);myConnection.Open();
2,用OracleClient串連Oracle
(1)添加System.Data.OracleClient程式集,然後再添加命名空間
using System.Data.OracleClient;
(2)串連資料庫:
string conString="Data Source=codematic;User Id=codeuser;Password=user123";OracleConnection myConnection=new OracleConnection(conString);myConnection.Open();
3,用MySqlConnection串連MySql
串連Mysql資料庫有兩種方法:MySQL Connection/ODBC和MysqlConnection/NET,ODBC連接器是符合ODBC標準的互動平台,是.net訪問mysql資料庫最好的選擇.
首先安裝Mysql-connection-net-xx.Data.msi這個組件.如果預設安裝可在目錄:c:\Program Files\MySQL\MySql Connection Net x.x.x\Binaries\.NET2.0找到Mysql.Data.dll,將檔案複製到bin目錄下,在項目中添加引用
(1)加入命名空間:
using MySql.Data.MySqlClient;
(2)串連資料庫:
string conString="server=127.0.0.1;database=mysql;user id=root;password=123";MySqlConnection myConnection=new MySqlConnection(conString);myConnection.Open();
4,用OleDbConnection串連各種資料來源
(1)加入命名空間:
using System.Data.OleDb;
(2)串連SQL Server:
string conString="Provider=SQLOLEDB.1;Persist Security Info=False; User ID=sa;Database=Codematic;Data Source=COMPUTER";OleDbConnection myConnection=new OleDbConnection(conString);myConnection.Open();
(3)串連Access
string conString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\Database1.mdb;Persist Security Info=False";
(4)串連Oracle
string conString="Provider=MSDAORA.1;User ID=user;Password=123;Data Source=db;Persist Security Info=False";
建立Command:
ado.net中的幾個對象