直接代码吧:
/// <summary> /// Oracle数据库连接 /// </summary> /// <param name="path">数据库连接串,例如:(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = IP)(PORT = *)))(CONNECT_DATA =(SERVICE_NAME = *)))</param> /// <param name="user">用户名</param> /// <param name="psw">用户密码</param> /// <returns>Oracle数据库连接对象</returns> private static OracleConnection ConnectOracle(string path, string user, string psw) { OracleConnection oracleConn = new OracleConnection(); string connectionStr = string.Format("Data Source={0};User Id={1};Password={2};", path, user, psw); oracleConn.ConnectionString = connectionStr; return oracleConn; } /// <summary> /// Access数据库连接 /// </summary> /// <param name="path">数据库连接串,例如:@"D:\qingdan.accdb;"</param> /// <param name="flag">Access版本,true为高于2007版</param> /// <returns>Access数据库连接对象</returns> private static OleDbConnection ConnectOracle(string path,bool flag) { OleDbConnection accessConn = null; if (string.IsNullOrWhiteSpace(path) || !File.Exists(path)) { MessageBox.Show("Acess数据库文件不存在", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return accessConn; } string conStr = null; if (flag) { conStr = "Provider=Microsoft.Ace.OleDb.12.0;"; conStr += "Data Source=" + path; conStr += ";Persist Security Info=False;"; } else { conStr = "Provider=Microsoft.Jet.OLEDB.4.0;"; conStr += "Data Source=" + path; } accessConn = new OleDbConnection(conStr); return accessConn; }
以上是建立数据库连接的方法。
对Oracle数据库的查询操作方法如下:
string sqlTest = "";//此处为查询SQL m_dsSet = new DataSet();//查询结果集合 OracleDataAdapter command = new OracleDataAdapter(sqlTest, m_oracleConn); command.Fill(m_dsSet, "ds");
对Access数据库操作:
OleDbCommand dbCommand = new OleDbCommand(); dbCommand.Connection = m_accessConn;//连接串 dbCommand.Transaction = m_accessConn.BeginTransaction(); dbCommand.CommandText ="";//操作命令SQL dbCommand.ExecuteNonQuery(); dbCommand.Transaction.Commit();