using
System.Data;
using System.Data.SqlClient;
..
string strConnection="user id=sa;password=;";
strConnection+="initial catalog=Northwind;Server=YourSQLServer;";
strConnection+="Connect Timeout=30";
SqlConnection
objConnection=new SqlConnection(strConnection);
..
objConnection.Open();
objConnection.Close();
using System.Data.OracleClient;
using System.Data;
//在窗體上添加一個按鈕,叫Button1,雙擊Button1,輸入以下代碼
private void
Button1_Click(object sender, System.EventArgs e)
{
string
ConnectionString="Data Source=sky;user=system;password=manager;";//寫連接串
OracleConnection conn=new OracleConnection(ConnectionString);//創(chuàng)建一個新連接
try
{
conn.Open();
OracleCommand
cmd=conn.CreateCommand();
cmd.CommandText="select * from
MyTable";//在這兒寫sql語句
OracleDataReader
odr=cmd.ExecuteReader();//創(chuàng)建一個OracleDateReader對象
while(odr.Read())//讀取數(shù)據(jù),如果odr.Read()返回為false的話,就說明到記錄集的尾部了
{
Response.Write(odr.GetOracleString(1).ToString());//輸出字段1,這個數(shù)是字段索引,具體怎么使用字段名還有待研究
}
odr.Close();
}
catch(Exception ee)
{
Response.Write(ee.Message); //如果有錯誤,輸出錯誤信息
}
finally
{
conn.Close(); //關(guān)閉連接
}
}
using MySQLDriverCS;
// 建立數(shù)據(jù)庫連接
MySQLConnection DBConn;
DBConn = new MySQLConnection(new
MySQLConnectionString("localhost","mysql","root","",3306).AsString);
DBConn.Open();
// 執(zhí)行查詢語句
MySQLCommand DBComm;
DBComm = new MySQLCommand("select Host,User from
user",DBConn);
// 讀取數(shù)據(jù)
MySQLDataReader DBReader
= DBComm.ExecuteReaderEx();
// 顯示數(shù)據(jù)
try
{
while (DBReader.Read())
{
Console.WriteLine("Host =
{0} and User = {1}", DBReader.GetString(0),DBReader.GetString(1));
}
}
finally
{
DBReader.Close();
DBConn.Close();
}
//關(guān)閉數(shù)據(jù)庫連接
DBConn.Close();