資料庫連線

hbhuang發表於2007-09-28
一.用SqlConnection連線SQL Server

1.加入名稱空間

using System.Data.SqlClient;

2.連線資料庫

SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "user id=sa;password=sinofindb;initial catalog=test;data source=127.0.0.1;Connect Timeout=30";
myConnection.Open();[@more@]


改進(更通用)的方法:

string MySqlConnection="user id=sa;password=sinofindb;Database =test;data source=127.0.0.1;Connect Timeout=30";
SqlConnection myConnection = new SqlConnection(MySqlConnection);
myConnection.Open();

二.用OleDbConnection連線

1.加入名稱空間

using System.Data.OleDb;


2.連線sql server

string MySqlConnection="Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=test;Integrated Security=SSPI;";

SqlConnection myConnection = new SqlConnection(MySqlConnection);
myConnection.Open();



3.連線Access(可透過建立.udl檔案獲得字串)

string MySqlConnection="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:db2000.mdb;

Persist Security Info=False;



4.連線Oracle(也可透過OracleConnection連線)

string MySqlConnection="Provider=MSDAORA;Data Source=db; user id=sa;password=sinofindb";

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10291852/viewspace-973359/,如需轉載,請註明出處,否則將追究法律責任。

相關文章