代码改变世界

C#操作Access数据库的例子

2008-07-08 16:08  Virus-BeautyCode  阅读(1493)  评论(1编辑  收藏  举报

using   System;   
  
using   System.Data;         
  
using   System.Data.OleDb;         
  
namespace   ADONETWriteQuery       
  
{     
              
///   <summary>     
              
///   Summary   description   for   Class1.     
              
///   </summary>     

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