C#中的ODBC、OLEDB连接

时间:2022-01-02 04:31:43
 

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)
        {
            //ODBC连接
            string conString = "DSN=tian"; //tian表示ODBC的用户数据源名
            string sql = "select count(*) from stuinfo"; //stuinfo为用户数据源绑定的数据库中的一个表
            OdbcConnection con = new OdbcConnection(conString);
            con.Open();
            OdbcCommand com = new OdbcCommand(sql, con);
            int i = Convert.ToInt32(com.ExecuteScalar());
            Console.WriteLine(i);


            //OLEDB连接
            string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\tian.mdb"; //连接Access数据库
            string sql = "select count(*) from stuinfo";  
            OleDbConnection con = new OleDbConnection(conString);
            con.Open();
            OleDbCommand com = new OleDbCommand(sql, con);
            int i = Convert.ToInt32(com.ExecuteScalar());
            Console.WriteLine(i);
        }
    }
}