C#荷恬SQLite

恬宀��札選利   竃侃��陣周嶄忽利   2014-11-05 19:21:19   堋響��1

C#荷恬SQLite
 
  SQLite 頁匯倖廼秘塀議選狼方象垂狼由��塰喘噴蛍鴻刑。壓匯乂方象楚音寄議塰喘 殻會嶄��邪泌塰喘 SQLite辛參自寄議週詰何俸扮議垢恬楚。 勣壓C#嶄塰喘 SQLite匆載児云��峪勣孀匯倖C#議wrapper祥辛參阻。峪勣芦廾匯和祥辛參塰喘阻��蒙艶圭宴。乎殻會弐栽 ADO.NET議号袈��旺拝屶嚇 Visual Studio議辛篇晒燕譜柴匂。參和頁栖徭利貧議匯倖箭徨��
  嬉蝕Visual Studio 2008��仟秀匯倖Console Application��葎緩�酊震躰�System.Data.SQLite議哈喘。耶紗匯倖方象垂銭俊��緩扮辛參窟状��仟秀銭俊嶄嗤阻匯倖SQLite Database Connection��薬僉緩窃侏議方象銭俊��旺拝仟秀匯倖猟周��
  test.db3. 俊和栖壓仟方象垂嶄耶紗匯嫖燕。

  和中蝕兵葎緩燕秀羨匯倖Data Access窃��參婢幣壓C#嶄泌採 塰喘 SQLite��辛參�誅鵤�才荷恬凪麿方象垂頁叱窄匯劔議��湖仍ADO.NET議孔斥。
  恷枠頁匯倖糞悶窃 Book.cs��
  public class Book
  {

    private int id;

    private string bookName;

     private decimal price; 
    public int ID
    {

      get { return id; }

       set { id = value; }
    }
    public string BookName
    {
      get { return bookName; }
      set { bookName = value; }
    }
    public decimal Price
    {
       get { return price; }
      set { price = value; }
    }
 } 


  園亟DAL窃��
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite; 
namespace ConsoleApplication1
{
    public class BookDAL
    {
        public static bool Create(Book book)
        {
            try
            {
                using (SQLiteConnection conn = new SQLiteConnection("Data Source=e:\\test.db3"))
                {
                    conn.Open();
                    SQLiteCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "insert into Book values(@ID,@BookName,@Price);";
                    cmd.Parameters.Add(new SQLiteParameter("ID", book.ID));
                    cmd.Parameters.Add(new SQLiteParameter("BookName", book.BookName));
                    cmd.Parameters.Add(new SQLiteParameter("Price", book.Price));
                    int i = cmd.ExecuteNonQuery();
                    return i == 1;
                }
            }
            catch (Exception)
            {
                //Do any logging operation here if necessary
                return false;
            }
        } 
        public static bool Update(Book book)
        {
            try
            {
                using (SQLiteConnection conn = new SQLiteConnection("Data Source=e:\\test.db3"))
                {
                    conn.Open();
                    SQLiteCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "update Book set BookName=@BookName,Price=@Price where ID=@ID;";
                    cmd.Parameters.Add(new SQLiteParameter("ID", book.ID));
                    cmd.Parameters.Add(new SQLiteParameter("BookName", book.BookName));
                    cmd.Parameters.Add(new SQLiteParameter("Price", book.Price));
                    int i = cmd.ExecuteNonQuery();
                    return i == 1;
                }
            }
            catch (Exception)
            {
                //Do any logging operation here if necessary
                return false;
            }
        } 
        public static bool Delete(int ID)
        {
            try
            {
                using (SQLiteConnection conn = new SQLiteConnection("Data Source=e:\\test.db3"))
                {
                    conn.Open();
                    SQLiteCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "delete from Book where ID=@ID;";
                    cmd.Parameters.Add(new SQLiteParameter("ID", ID));         
                    int i = cmd.ExecuteNonQuery();
                    return i == 1;
                }
            }
            catch (Exception)
            {
                //Do any logging operation here if necessary
                return false;
            }
        } 
        public static Book GetbyID(int ID)
        {
            try
            {
                using (SQLiteConnection conn = new SQLiteConnection("Data Source=e:\\test.db3"))
                {
                    conn.Open();
                    SQLiteCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "select * from Book where ID=@ID;";
                    cmd.Parameters.Add(new SQLiteParameter("ID", ID));
                    SQLiteDataReader dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        Book book = new Book();
                        book.ID = dr.GetInt32(0);
                        book.BookName = dr.GetString(1);
                        book.Price = dr.GetDecimal(2);
                        return book;
                    }
                    else
                        return null;
                }
            }
            catch (Exception)
            {
                //Do any logging operation here if necessary
                return null;
            }
        }
    }
} 
  園亟霞編麼殻會��
using System; 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Book book = new Book();
            book.ID = 1;
            book.BookName = "Book A";
            book.Price = 10.0m;
            BookDAL.Create(book); 
            book.ID = 2;
            book.BookName = "及屈云慕";
            book.Price = 13.0m;
            BookDAL.Create(book); 
            book = BookDAL.GetbyID(2);
            Console.WriteLine(book.ID + " " + book.BookName + " " + book.Price); 
            book.Price = 11.1m;
            BookDAL.Update(book); 
            book = BookDAL.GetbyID(2);
            Console.WriteLine(book.ID + " " + book.BookName + " " + book.Price);
            book = BookDAL.GetbyID(1);
            Console.WriteLine(book.ID + " " + book.BookName + " " + book.Price); 
        }
    }
} 

 

Copyright© 2006-2015 ComponentCN.com all rights reserved.嶷伯斗冫親室嗤�濤�望(陣周嶄忽利) 井幡侭嗤 嚆ICP姥12000264催 隈舵綱諒�犀�応舵弗並暦侭
人捲罷周
live chat