C#连接ACCESS和SQL2000

80酷酷网    80kuku.com

  access 

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace ConsoleApplication1
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 class Class1
 {
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {

//以下是连接Access
   OleDbConnection dbconn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data                      source=c:\a.mdb");
   dbconn.Open();
   //OleDbCommand cmd=new OleDbCommand("select * from test1",dbconn);

    OleDbCommand cmd=dbconn.CreateCommand();
   cmd.CommandText="select * from test1";
   cmd.CommandType=CommandType.Text;
   OleDbDataReader reader=cmd.ExecuteReader();

//以下是连接SQL2000
   /*SqlConnection conn=new SqlConnection("server=localhost;Initial catalog=NorthWind;user  ID=sa;password=;");
   conn.Open();
   SqlCommand cmd=conn.CreateCommand();
   cmd.CommandText="select * from Customers";
   cmd.CommandType=CommandType.Text;
   SqlDataReader reader=cmd.ExecuteReader(CommandBehavior.CloseConnection);*/

   string output;
   while(reader.Read())
   {
    output=string.Format("custormer {0}:{1} works for {2}",reader.GetString(1),reader.GetString(2),reader.GetString(3));
    Console.WriteLine(output);
   }
  }
 }
}


分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: