日期:2009-09-09  浏览次数:20402 次

using System.Data.OleDb;

新建一个类(用于数据库连接的参数):

public static string GetOracleDbConnStr()

{
// 数据库连接
string tempUser="system",tempPassword="andystarmkmk",tempDataSource="andy";
string tempstrCon="Provider=OraOLEDB.Oracle.1;Persist Security Info=False;"+
"User ID="+tempUser+";Password="+tempPassword+";Data Source="+tempDataSource;
return tempstrCon;
}

设置Click事件:


private void button1_Click(object sender, System.EventArgs e)
{
string strSQL="select * from scott.aa";
string tempstrCon=Form1.GetOracleDbConnStr();
string tempstrCom="select * from scott.aa";
OleDbConnection tempmyConn=new OleDbConnection(tempstrCon);
DataSet tempmyDataSet=new DataSet();

OleDbDataAdapter tempmyCommand=new OleDbDataAdapter (tempstrCom,tempmyConn);
tempmyCommand.Fill(tempmyDataSet);
tempmyConn.Open();
dataGrid1.DataSource =tempmyDataSet;



OleDbCommand tempCommand=new OleDbCommand(strSQL,tempmyConn);
tempCommand.ExecuteNonQuery();
tempmyConn.Close();

}

注意:

1.连接数据库需要的参数:

◎OleDbConnection 用于建立连接

◎DataSet 数据在内存中的缓存,就是在内存中建立一个与数据库一致的表

◎OleDbDataAdapter 用于更新数据源

◎OleDbCommand 用于执行SQL命令

2.连接完成后应该马上关闭连接。