C#连接ACCESS和SQL2000

类别:.NET开发 点击:0 评论:0 推荐:

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);
   }
  }
 }
}

本文地址:http://com.8s8s.com/it/it40913.htm