日期:2014-05-19  浏览次数:20846 次

C#怎么读取orcle数据库的内容
刚学C#很多不懂,我想问一下.怎么用C#去连接和读取oracle数据库的内容.
  就好像是我做一个登陆界面,我要从数据库里读取用户名和密码,以验证是否可以登陆。

------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OracleClient;
using System.Data;

namespace Common
{
public class DataAccess
{
private string _connectionStr = " ";
private string _userID = " ";
private OracleConnection _conn = null;
private OracleCommand _comm = null;
private OracleDataAdapter _da = null;

public DataAccess(string dataSource,string userID,string password)
{
this._userID = userID;
this._connectionStr = "data source= "+dataSource+ ";user id= "+userID+ ";password= "+password+ "; ";
this._conn = new OracleConnection(this._connectionStr);
this._comm = new OracleCommand();
this._comm.CommandType = CommandType.Text;
this._comm.Connection = this._conn;
this._da = new OracleDataAdapter();
this._da.SelectCommand = this._comm;
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
this._conn.Close();
}
/// <summary>
/// 验证用户登陆是否成功
/// </summary>
/// <returns> </returns>
public bool Login()
{
bool returnValue = true;
try
{
this._conn.Open();
}
catch
{
returnValue = false;
}
finally
{
if (this._conn.State == ConnectionState.Open)
{
this._conn.Close();
}
}
return returnValue;
}
}
}

------解决方案--------------------
晕,那问题就大了。
首先connection用来连接数据库
需要会用command命令执行sql命令
还有Reader读取器可以读取表中的内容。。。。。。
等等
如果你使用控件,比如vs2005中的,sqlDataSource,那可以不写一行代码就可以显示数据库表中的内容了(支持oracle)。
还是推荐你一本入门书,有中文版的,C#数据库入门(beginning ado.net in c #)