C#连接ACCESS和SQL2000

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

using System; using System.Data; using System.Configuration; using System.Data.OleDb; namespace xxxxx { /// <summary> /// Access数据库访问类 /// </summary> public class DataAccess { private static OleDbConnection conn = new OleDbConnection(); private static OleDbCommand comm = new OleDbCommand(); private static string asscessPath = @".\PoliScan.mdb"; /// <summary> /// 设置Access数据库路径 /// </summary> /// <param name="path">完整的路径字符串</param> public static void SetAccessPath(string path) { asscessPath = path; } /// <summary> /// 打开数据库 /// </summary> /// <returns>成功返回 true;失败返回 false;异常时抛出异常</returns> public static bool OpenConnection() { bool retval = false; if (conn.State != ConnectionState.Open) { conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + asscessPath; comm.Connection = conn; try { conn.Open(); if (conn.State == ConnectionState.Open) { retval = true; } } catch (Exception e) { throw new Exception(e.Message); } ............................ ............................ using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; using System.Data.SqlClient; /// <summary> /// DataBase 的摘要说明 /// </summary> public class DataBase : IDisposable { public DataBase() { // // TODO: 在此处添加构造函数逻辑 // } private SqlConnection con; //创建连接对象 #region 打开数据库连接 /// <summary> /// 打开数据库连接. /// </summary> private void Open() { // 打开数据库连接 if (con == null) { con = new SqlConnection("Data Source = . ;Database = CET ;Integrated Security = SSPI "); } if (con.State == System.Data.ConnectionState.Closed) con.Open(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值