斌's profile技术员的共享空间PhotosBlogLists Tools Help

Blog


    June 23

    ADO.Net对Oracle数据库的操作

    OracleConnection 对象

    要访问一个数据源,你必须先建立一个到它的连接。这个连接里描述了数据库服务器类型、数据库名字、用户名、密码,和连接数据库所需要的其它参数。command对象通过使用connection对象来知道是在哪个数据库上面执行ORACLE命令。

      OracleConnection oracleConn = new OracleConnection();
    oracleConn.ConnectionString 
    = "User Id=scott;Password=tiger;Data Source=oracleSN;";
    oracleConn.Open();

    OracleCommand对象

    连接数据库后就可以开始想要执行的数据库操作,这个是通过command对象完成,command对象一般被用来发送ORACLE语句给数据库。command对象通过connection对象得知道应该与哪个数据库进行连接。我们既可以用command对象来直接执行ORACLE命令,也可以将一个command对象的引用传递给OracleDataAdapterOracleDataAdapter能包含一系列的command对象,可以处理大量数据。

     
    public void ReadMyData(string connectionString)
    {
        
    string queryString = "SELECT EmpNo, DeptNo FROM Scott.Emp";
        OracleConnection connection 
    = new OracleConnection(connectionString)
        
    {
            OracleCommand command 
    = new OracleCommand(queryString, connection);
            connection.Open();
            OracleDataReader reader 
    = command.ExecuteReader();
            
    try
            
    {
                
    while (reader.Read())
                
    {
                    Console.WriteLine(reader.GetInt32(
    0+ "" + reader.GetInt32(1));
                }

            }

            
    finally
            
    {
                reader.Close();
            }

        }

    }

    OracleDataReader对象

    许多数据库操作要求我们仅仅只是需要读取一组数据。这时候就用到了data reader对象。通过data reader对象,我们可以获得从command对象的SELECT语句得到的结果。考虑到性能方面的因素,data reader返回的数据流被设计为只读的、单向的,这将意味着你只能按照一定的顺序从数据流中取出数据。虽然你在这里也获得了性能上的提升,但是缺点也是明显的,不能够操作取回数据,如果需要操作编辑数据,解决的办法是使用DataSet
    OracleDataReader提供了几个方法,在读取数据的时候用这些方法可以对数据表中的数据按数据类型进行筛选GetDateTime, GetDouble, GetGuid, GetInt32

    DataSet对象

    DataSet对象用于表示那些储存在内存中的数据。它包括多个DataTable对象,DataTable就象一个普通的数据库中的表一样,也有行和列,我们甚至能够通过定义表和表之间的关系来创建从属关系。DataSet主要用于管理存储在内存中的数据以及对数据的断开操作。注意,由于DataSet对象能被所有Data Providers(数据源交互的类库)使用,它不需要指定前缀。

    OracleDataAdapter对象

    某些时候我们只需要读数据,并且你不需要修改它们把更改写回数据源。但是还有这样一些情况为了减少数据库调用的次数,我们把数据缓存在内存中。Data adapter通过断开模型来轻松的实现了后面这种情况的处理。当批量完成的对数据库的读写操作的并将改变写回数据库的时候,data adapter 会填充(fillDataSet对象。data adaapter里包含了connection对象,当对数据源进行读取或者写入的时候,data adapter会自动的打开或者关闭连接。此外,data adapter还包含对数据的SELECT,INSERT,UPDATEDELETE操作的command对象引用。如果我们为DataSet中的每一个table都指定data adapter,它将会帮你处理好所有与连接处理数据库的操作,我们所需要做的仅仅就是告诉data adapter什么时候读取或者写入到数据库。


    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://hb1984.spaces.live.com/blog/cns!B71DA5098812F698!121.trak
    Weblogs that reference this entry
    • None