如何使用C#操作access数据库,我要全部的代码,如果access 的文件后缀不是MDB会不会对操作有影响?

解决方案 »

  1.   

    using   System;   
      using   System.Data;         
      using   System.Data.OleDb;         
      namespace   ADONETWriteQuery       
      {     
     
                  class   Class1     
                  {       
                              static   void   Main(string[]   args)       
                              {       
                                          string   strDSN   =   "Provider=Microsoft.Jet.OLEDB.4.0;Data   
      Source=c:\\mcTest.MDB";       
                                          string   strSQL   =   "INSERT   INTO   Developer(Name,   Address   )   VALUES(   
      'NewName',   'NewAddress')"   ;       
                                              
                                          //   create   Objects   of   ADOConnection   and   ADOCommand         
                                          OleDbConnection   myConn   =   new   OleDbConnection(strDSN);       
                                          OleDbCommand   myCmd   =   new   OleDbCommand(   strSQL,   myConn   );       
                                          try       
                                          {       
                                                      myConn.Open();       
                                                      myCmd.ExecuteNonQuery();       
                                          }       
                                          catch   (Exception   e)       
                                          {       
                                                      Console.WriteLine("Oooops.   I   did   it   again:\n{0}",   e.Message);       
                                          }       
                                          finally       
                                          {       
                                                      myConn.Close();       
                                          }       
              
              
              
                              }     
                  }       
      }       
      

  2.   

    用access操作类
    public static string connectionString = "";
    public static OleDbDataReader ExecuteReader(string strSQL)   
      {   
      OleDbConnection connection = new OleDbConnection(connectionString);   
      OleDbCommand cmd = new OleDbCommand(strSQL, connection);   
      try   
      {   
      connection.Open();   
      OleDbDataReader myReader = cmd.ExecuteReader();   
      return myReader;   
      }   
      catch (System.Data.OleDb.OleDbException e)   
      {   
      throw new Exception(e.Message);   
      }   
      }   
    public static DataSet Query(string SQLString)   
      {   
      using (OleDbConnection connection = new OleDbConnection(connectionString))   
      {   
      DataSet ds = new DataSet();   
      try   
      {   
      connection.Open();   
      OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection);   
      command.Fill(ds, "ds");   
      }   
      catch (System.Data.OleDb.OleDbException ex)   
      {   
      throw new Exception(ex.Message);   
      }   
      return ds;   
      }   
      }   
    http://topic.csdn.net/u/20090510/18/88115B15-BAC2-4A8E-B801-1B927B800A34.html
      

  3.   

    access数据库的后缀名不是mdb没有关系。
    直接下个动软的代码生成器,就可以生成多数据库的demo
      

  4.   

    其实只要了解了ado.net,什么数据库都差不多,就那几个对象
      

  5.   

    用C#访问Access数据库