zoukankan      html  css  js  c++  java
  • 【C#】Creating/Querying/Modifying the .mdb databases

    As for databases, there are quit many kinds such as foxpro, mysql, sqlserver, etc.

    But when we need to build a project which should be portable, the .mdb(Access) databases may be one of the best choices.

    Here we go.:)

    1.Creating a .mdb database

    Actually this simple question really got me at the beginning.But it can be easy in another way.

    Firstly, we should adduct the libraries —— 'Microsoft ActiveX Data Objects 2.8 Library' and 'Microsoft ADO Ext. 2.8 for DDL and Security'.

    Then,put a 'using ADOX;'.

    After that you can create a database like this:

    1 string filename=@"c:1.mdb";
    2 string phrase = "provider=Microsoft.Jet.OLEDB.4.0;data source="" + filename + """;
    3 (new Catalog()).Create(phrase);

    2.Querying a .mdb database

    This is much easier than that in VB6 because almost every function has been supported by the .Net Framework.

    Firstly, we need to connect to the database and then query it.

    After All,WE NEED TO CLOSE IT!!!!!!

    Like this:

     1 string filename = @"c:1.mdb";
     2 string pwd = "1234";
     3 string phrase = "provider=Microsoft.Jet.OLEDB.4.0;data source="" + filename + """;
     4 // add password if neccessary
     5 if (pwd != null) phrase += "; Jet OLEDB:Database Password=" + (string)pwd;
     6 OleDbConnection conn = new OleDbConnection(phrase);
     7 conn.Open();  //very important, don't forget it
     8 
     9 string sql = "select * from database";  //sql sentence
    10 OleDbDataAdapter oda = new OleDbDataAdapter(sql, conn);
    11 
    12 DataTable dt1 = new DataTable(); oda.Fill(dt1); //get table of data
    13 
    14 foreach (DataRow dr in dt1.Rows)
    15 {
    16     foreach (DataColumn dc in dt1.Columns)  //print every rows
    17     {
    18         console.Write((string)dr[dc]+" ");  //print every columns
    19     }
    20     console.Writeln();
    21 }
    22 
    23 conn.close();

    3.Modifying a .mdb database

    It can be quit similiar to the Quering Process.The main difference is the class we use is ( OleDbCommand ) now.

    Coding like this:

     1 string filename = @"c:1.mdb";
     2 string pwd = "1234";
     3 string phrase = "provider=Microsoft.Jet.OLEDB.4.0;data source="" + filename + """;
     4 // add password if neccessary
     5 if (pwd != null) phrase += "; Jet OLEDB:Database Password=" + (string)pwd;
     6 OleDbConnection conn = new OleDbConnection(phrase);
     7 conn.Open();  //very important, don't forget it
     8 
     9 string sql = "update database set abc='123' where bcd='234'";  //sql sentence
    10 OleDbCommand comm = new OleDbCommand(sql, conn);
    11 comm.ExecuteNonQuery();  //the feedback value is the number of rows it actually processed.
    12 
    13 conn.close();
  • 相关阅读:
    数据结构与算法(3-4)--矩阵的压缩存储
    数据结构与算法(3-3)--队列的应用
    数据结构与算法(3-2)--栈的应用
    数据结构与算法(3-1)--栈和队列
    数据结构与算法(2)--线性表(数组和链表)
    数据结构与算法(1)--时间及空间复杂度
    python变量与地址的关系
    python高级(03)--socket编程
    python高级(02)--生成器和迭代器
    python处理http接口请求
  • 原文地址:https://www.cnblogs.com/Fefnir/p/6285221.html
Copyright © 2011-2022 走看看