Chinaunix首页 | 论坛 | 博客
  • 博客访问: 206177
  • 博文数量: 47
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1259
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-24 10:20
文章分类
文章存档

2014年(21)

2013年(26)

分类: C#/.net

2014-03-10 17:36:52

///
/// Oracle数据库连接
///

/// 数据库连接串,例如:(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = IP)(PORT = *)))(CONNECT_DATA =(SERVICE_NAME = *)))
/// 用户名
/// 用户密码
/// Oracle数据库连接对象
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;
}
///
/// Access数据库连接
///

/// 数据库连接串,例如:@"D:\qingdan.accdb;"
/// Access版本,true为高于2007版
/// Access数据库连接对象
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();
阅读(3314) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~