首页 > 数据库 > Access > 正文

C#连接ACCESS和SQL2000

2024-09-07 19:04:55
字体:
来源:转载
供稿:网友
 

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表