ADO.NET訪問Access(文本數據庫)數據操作(CRUD)


1,ADO.NET訪問Access(文本數據庫)數據操作(CRUD)

 

2,DatabaseDesign

文本數據庫Northwind.mdb

3,/App_Code

 3.1,/App_Code/DBConnection.cs

View Code
//引用Access文本數據的類
using System.Data.OleDb;

/// <summary>
/// DBConnection 的摘要說明
/// </summary>
public class DBConnection
{
    OleDbConnection con = null;

   
    public DBConnection()
    {        
        con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:\\northwind.mdb;");
    }

    public OleDbConnection Con
    {
        get { return con; }
        set { con = value; }
    }

}

3.2,/App_Code/ProductsInfo.cs

View Code
/// <summary>
/// ProductsInfo 的摘要說明
/// </summary>
public class ProductsInfo
{
    //字段
    int productid;
    string productname;    
    decimal unitprice;       
    int categoryid;
    

    public ProductsInfo()
    {
        //
        // TODO: 在此處添加構造函數邏輯
        //
    }
    //插入商品
    public ProductsInfo(string productname, decimal unitprice, int categoryid)
    {
        this.productname = productname;
        this.unitprice = unitprice;
        this.categoryid = categoryid;
    }
    //查看全部
    //插入商品
    public ProductsInfo(int productid, string productname, decimal unitprice, int categoryid)
    {
        this.productid = productid;
        this.productname = productname;
        this.unitprice = unitprice;
        this.categoryid = categoryid;
    }
    //封裝
    public int Productid
    {
        get { return productid; }
        set { productid = value; }
    }
    public string Productname
    {
        get { return productname; }
        set { productname = value; }
    }
    public decimal Unitprice
    {
      get { return unitprice; }
      set { unitprice = value; }
    }
    public int Categoryid
    {
        get { return categoryid; }
        set { categoryid = value; }
    }
}

3.3,/App_Code/ProductsOper.cs  (注意在做刪除做作時,請在delete之后添加關鍵詞“from”)

View Code
using System.Data.OleDb;
using System.Collections.Generic;
/// <summary>
/// ProductsOper 的摘要說明
/// </summary>
public class ProductsOper
{
    public ProductsOper()
    {
        //
        // TODO: 在此處添加構造函數邏輯
        //
    }
    //插入商品
    public static void Insert(ProductsInfo product)
    {
        string sql = "insert into 產品(產品名稱,單價,類別ID) values(@productname,@unitprice,@categoryid)";

        OleDbConnection con = new DBConnection().Con;
        OleDbCommand com = new OleDbCommand();

        com = con.CreateCommand();
        com.CommandText = sql;
        //配參
        com.Parameters.Add(new OleDbParameter("@productname", product.Productname));
        com.Parameters.Add(new OleDbParameter("@unitprice", product.Unitprice));
        com.Parameters.Add(new OleDbParameter("@categoryid", product.Categoryid));
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
    }
    //查所有產品
    public static IList<ProductsInfo> GetProductByCateid(int cateid)
    {
        string sql = "select 產品ID,產品名稱,單價,類別ID from 產品 where 類別ID=@cateid";
        IList<ProductsInfo> products = new List<ProductsInfo>();

        OleDbConnection con = new DBConnection().Con;
        OleDbCommand com = new OleDbCommand();
        com = con.CreateCommand();


        com.Parameters.Add(new OleDbParameter("@cateid", cateid));
        com.CommandText = sql;

        con.Open();

        OleDbDataReader oddr = com.ExecuteReader();
        while (oddr.Read())
        {
            ProductsInfo product = new ProductsInfo(oddr.GetInt32(0), oddr.GetString(1), oddr.GetDecimal(2), oddr.GetInt32(3));
            products.Add(product);
        }
        con.Close();

        return products;
    }
    //根據productid刪除
    public static void Delete(int productid)
    {
        string sql = "delete from 產品 where 產品ID=@productid";

        OleDbConnection con=new DBConnection().Con;
        OleDbCommand com=new OleDbCommand();

        com=con.CreateCommand();
        //配參
        com.Parameters.Add(new OleDbParameter("@productid", productid));

        com.CommandText=sql;
        con.Open();

        com.ExecuteNonQuery();
        con.Close();

    }

    ////根據productid更新數據
    public static void Update(ProductsInfo prod)
    {

        string sql = "update 產品 set 產品名稱=@productname,單價=@unitprice,類別ID=@categoryid "
            +" where 產品ID=@productid";

        OleDbConnection con = new DBConnection().Con;
        OleDbCommand com = new OleDbCommand();

        com = con.CreateCommand();
        com.CommandText = sql;
        //配參
        com.Parameters.Add(new OleDbParameter("@productname", prod.Productname));
        com.Parameters.Add(new OleDbParameter("@unitprice", prod.Unitprice));
        com.Parameters.Add(new OleDbParameter("@categoryid", prod.Categoryid));
        com.Parameters.Add(new OleDbParameter("@productid", prod.Productid));
        con.Open();

        com.ExecuteNonQuery();
        con.Close();
    }
}
4,功能截圖

ado+access

5,源代碼下載 

 http://ylbtechdotnet.googlecode.com/files/ADO-%E6%9D%82%E9%A1%B9.zip

warn 作者:ylbtech
出處:http://ylbtech.cnblogs.com/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM