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);//创建一个新连接
try
{
conn.Open();
OracleCommand
cmd=conn.CreateCommand();
cmd.CommandText="select * from
MyTable";//在这儿写sql语句
OracleDataReader
odr=cmd.ExecuteReader();//创建一个OracleDateReader对象
while(odr.Read())//读取数据,如果odr.Read()返回为false的话,就说明到记录集的尾部了
{
Response.Write(odr.GetOracleString(1).ToString());//输出字段1,这个数是字段索引,具体怎么使用字段名还有待研究
}
odr.Close();
}
catch(Exception ee)
{
Response.Write(ee.Message); //如果有错误,输出错误信息
}
finally
{
conn.Close(); //关闭连接
}
}
using MySQLDriverCS;
// 建立数据库连接
MySQLConnection DBConn;
DBConn = new MySQLConnection(new
MySQLConnectionString("localhost","mysql","root","",3306).AsString);
DBConn.Open();
// 执行查询语句
MySQLCommand DBComm;
DBComm = new MySQLCommand("select Host,User from
user",DBConn);
// 读取数据
MySQLDataReader DBReader
= DBComm.ExecuteReaderEx();
// 显示数据
try
{
while (DBReader.Read())
{
Console.WriteLine("Host =
{0} and User = {1}", DBReader.GetString(0),DBReader.GetString(1));
}
}
finally
{
DBReader.Close();
DBConn.Close();
}
//关闭数据库连接
DBConn.Close();