標籤:
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