日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

C#中的ODBC、OLEDB連接

系統(tǒng) 2864 0

原文: C#中的ODBC、OLEDB連接

?

      
        using
      
      
         System;


      
      
        using
      
      
         System.Collections.Generic;


      
      
        using
      
      
         System.Text;


      
      
        using
      
      
         System.Data.Odbc;


      
      
        using
      
      
         System.Data.OleDb;




      
      
        namespace
      
      
         ODBCTest

{

    
      
      
        class
      
      
         Program

    {

        
      
      
        static
      
      
        void
      
       Main(
      
        string
      
      
        [] args)

        {

            Console.WriteLine(
      
      
        "
      
      
        ##################################ODBC連接1#############################
      
      
        "
      
      
        );



            
      
      
        using
      
       (OdbcConnection conn = 
      
        new
      
       OdbcConnection(
      
        "
      
      
        driver={SQL Server};SERVER=127.0.0.1;UID=sa;PWD=Abc123123;DATABASE=booking
      
      
        "
      
      
        ))

            {

                conn.Open();

                OdbcCommand cmd 
      
      =
      
         conn.CreateCommand();

                cmd.CommandText 
      
      = 
      
        "
      
      
        select  * from TEST
      
      
        "
      
      
        ;

                
      
      
        using
      
       (OdbcDataReader reader =
      
         cmd.ExecuteReader())

                {

                    Console.WriteLine(
      
      
        "
      
      
        字段數(shù):\t{0}
      
      
        "
      
      
        , reader.FieldCount);

                    Console.WriteLine(
      
      
        "
      
      
        ==============數(shù)據(jù)內(nèi)容===========
      
      
        "
      
      
        );

                    
      
      
        while
      
      
         (reader.Read())

                    {

                        Console.WriteLine(
      
      
        "
      
      
        ID: {0}\tTitle: {1}
      
      
        "
      
      , reader.GetInt32(
      
        0
      
      ).ToString(), reader.GetString(
      
        1
      
      
        ));

                    }

                }

            }

            Console.WriteLine(
      
      
        "
      
      
        ################################ODBC連接2###############################
      
      
        "
      
      
        );



            
      
      
        string
      
       conString = 
      
        "
      
      
        Dsn=ggyy1;
      
      
        "
      
       + 
      
        "
      
      
        Uid=sa;
      
      
        "
      
       + 
      
        "
      
      
        Pwd=Abc123123;
      
      
        "
      
      ;
      
        //
      
      
        Dsn表示在ODBC創(chuàng)建的用戶數(shù)據(jù)源名
      
      
        

            OdbcConnection con 
      
      = 
      
        new
      
      
         OdbcConnection(conString);

            
      
      
        string
      
       sql = 
      
        "
      
      
        select * from TEST
      
      
        "
      
      ; 
      
        //
      
      
        stuinfo為用戶數(shù)據(jù)源綁定的數(shù)據(jù)庫中的一個表
      
      
        

            con.Open();

            OdbcCommand com 
      
      = 
      
        new
      
      
         OdbcCommand(sql, con);

            
      
      
        using
      
       (OdbcDataReader reader =
      
         com.ExecuteReader())

            {

                Console.WriteLine(
      
      
        "
      
      
        字段數(shù):\t{0}
      
      
        "
      
      
        , reader.FieldCount);

                Console.WriteLine(
      
      
        "
      
      
        ==============數(shù)據(jù)內(nèi)容===========
      
      
        "
      
      
        );

                
      
      
        while
      
      
         (reader.Read())

                {

                    Console.WriteLine(
      
      
        "
      
      
        ID: {0}\tTitle: {1}
      
      
        "
      
      , reader.GetInt32(
      
        0
      
      ).ToString(), reader.GetString(
      
        1
      
      
        ));

                }

            }

            Console.WriteLine();



            Console.WriteLine(
      
      
        "
      
      
        ##################################OLEDB連接#############################
      
      
        "
      
      
        );



            
      
      
        string
      
       conString1 = 
      
        @"
      
      
        Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\test.mdb
      
      
        "
      
      ; 
      
        //
      
      
        連接Access數(shù)據(jù)庫
      
      
        string
      
       sql1 = 
      
        "
      
      
        select count(*) from TEST
      
      
        "
      
      
        ;

            OleDbConnection con1 
      
      = 
      
        new
      
      
         OleDbConnection(conString1);

            con1.Open();

            OleDbCommand com1 
      
      = 
      
        new
      
      
         OleDbCommand(sql1, con1);

            
      
      
        int
      
       i =
      
         Convert.ToInt32(com1.ExecuteScalar());

            Console.WriteLine(i);

            Console.Read();

        }

    }

}
      
    

?

C#中的ODBC、OLEDB連接


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 临湘市| 宿州市| 正镶白旗| 彭山县| 奈曼旗| 桃源县| 得荣县| 宁南县| 西丰县| 依安县| 乐昌市| 盐津县| 屏东县| 正宁县| 彭水| 济南市| 灵璧县| 天水市| 耒阳市| 白城市| 慈利县| 延长县| 鲁山县| 垫江县| 延安市| 巴彦县| 平阴县| 灵宝市| 花垣县| 那坡县| 驻马店市| 铜陵市| 贵溪市| 武乡县| 抚松县| 西安市| 鄢陵县| 调兵山市| 永清县| 荔波县| 寻乌县|