c#操作SQLIte

來源:互聯網
上載者:User

標籤:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.SQLite;using System.Configuration;using System.Collections;using System.Data;using System.IO;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;using System.Windows;using System.Windows.Controls;namespace Dell{    public class Ops    {        public static string dbPath = "Data Source =" + Environment.CurrentDirectory + "/test.db";        private SQLiteCommand command = new SQLiteCommand();        private SQLiteConnection connection = null;        public Ops() { }       //重載建構函式        public  Ops(string dbPath)        {            initCon(dbPath);            Boolean b = judgeConn();            if( b == false)            {                MessageBox.Show("資料庫開啟失敗!");            }        }        //建立connection ,並返回連結             public static SQLiteConnection buildConn()        {            SQLiteConnection conn = new SQLiteConnection(dbPath);//建立資料庫執行個體,指定檔案位置            conn.Open();//開啟資料庫,若檔案不存在會自動建立            return conn;        }        //建立connection,不開啟        public  void initCon(string dbPath)        {            connection = new SQLiteConnection(dbPath);        }        //判斷串連狀態        private Boolean judgeConn()        {            Boolean isOpen = true;            if (connection.State != ConnectionState.Open)            {                try                {                    //initCon(dbPath);                    connection.Open();                }catch(Exception){                    isOpen = false;                }            }            return isOpen;                    }        //抽象改變表資料的命令執行函數        public int ChangeCommand(string commandText, SQLiteParameter[] commandParameters)        {            int changedNo = 0;            judgeConn();            command.Connection = connection;            command.CommandText = commandText;            if (commandParameters != null)            {                command.Parameters.AddRange(commandParameters);                changedNo = command.ExecuteNonQuery();            }            else            {                changedNo = command.ExecuteNonQuery();            }            return changedNo;        }        //抽象出不改變表資料的函數        public SQLiteDataReader noChangeCommand(string commandText)        {                        judgeConn();            //command.Connection = connection;            //command.CommandText = commandText;            SQLiteCommand command = new SQLiteCommand(commandText, connection);            SQLiteDataReader sdr = command.ExecuteReader();               return sdr;        }         }}

 

c#操作SQLIte

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.