SQLiteHelper

SQLiteHelper 帮助类 [一]

SQLiteHelper 帮助类 [二]

SQLiteHelper 帮助类 [三]

 

View Code
  1 using System;

  2 using System.Collections.Generic;

  3 using System.Linq;

  4 using System.Text;

  5 using System.Data.SQLite;//

  6 using System.Data;//

  7 using System.IO;//

  8 using System.Data.Common;//

  9 using System.Configuration;//

 10 

 11 namespace SQLiteHelper

 12 {

 13     public class SQLiteHelper41

 14     {

 15         #region 拼接字符串 http://www.cnblogs.com/bytime/archive/2011/10/27/2226004.html

 16 

 17         private string connStr = "";//SQLite数据库连接字符串

 18         private static string dbPathDefault = System.Environment.CurrentDirectory + "\\DB\\";//默认 目录的 路径

 19 

 20         public SQLiteHelper41(string dbName, bool isFullPath)

 21         {

 22             if (isFullPath)

 23             {

 24                 connStr = @"Data Source=" + dbName + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10";

 25             }

 26             else

 27             {

 28                 connStr = @"Data Source=" + dbPathDefault + dbName + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10";

 29             }

 30         }

 31 

 32         /// <summary>

 33         /// 功能: 创建数据库,带路径

 34         /// </summary>

 35         /// <param name="dbName"></param>

 36         /// <param name="isFullPath"></param>

 37         public void CreateDB(string dbName, bool isFullPath)

 38         {

 39             if (isFullPath)

 40             {

 41                 if (!File.Exists(dbName))

 42                 {

 43                     SQLiteConnection.CreateFile(dbName);

 44                 }

 45             }

 46             else

 47             {

 48 

 49                 if (!Directory.Exists(dbPathDefault))

 50                 {

 51                     //检测 要创建的SQLite数据库所在 目录是否存在 若不存在则创建;否则继续;

 52                     Directory.CreateDirectory(dbPathDefault);

 53                 }

 54                 if (!File.Exists(dbPathDefault + dbName))

 55                 {

 56                     SQLiteConnection.CreateFile(System.Environment.CurrentDirectory + "\\DB\\" + dbName);

 57                 }

 58             }

 59         }

 60 

 61         /// <summary>

 62         /// 功能: 执行sql,不返回

 63         /// </summary>

 64         /// <param name="sqlStr">要执行的sql</param>

 65         public void ExecuteSql(string sqlStr)

 66         {

 67             using (DbConnection conn = new SQLiteConnection(connStr))

 68             {

 69                 conn.Open();

 70                 DbCommand comm = conn.CreateCommand();

 71                 comm.CommandText = sqlStr;

 72                 comm.CommandType = CommandType.Text;

 73                 comm.ExecuteNonQuery();

 74             }

 75         }

 76 

 77         /// <summary>

 78         /// 功能: 执行sql语句数组

 79         /// </summary>

 80         /// <param name="sqlStr"></param>

 81         public void ExecuteSqlList(string[] sqlStr)

 82         {

 83             using (DbConnection conn = new SQLiteConnection(connStr))

 84             {

 85                 conn.Open();

 86                 DbCommand comm = conn.CreateCommand();

 87                 foreach (string item in sqlStr)

 88                 {

 89                     comm.CommandText = item;

 90                     comm.CommandType = CommandType.Text;

 91                     comm.ExecuteNonQuery();

 92                 }

 93             }

 94         }

 95 

 96         /// <summary>

 97         /// 功能: 执行sql返回deteset

 98         /// </summary>

 99         /// <param name="sqlStr"></param>

100         /// <returns></returns>

101         public DataSet ExecDataSet(string sqlStr)

102         {

103             using (SQLiteConnection conn = new SQLiteConnection(connStr))

104             {

105                 conn.Open();

106                 SQLiteCommand cmd = conn.CreateCommand();

107                 cmd.CommandText = sqlStr;

108                 cmd.CommandType = CommandType.Text;

109 

110                 SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);

111                 DataSet ds = new DataSet();

112                 da.Fill(ds);

113 

114                 return ds;

115             }

116         }

117 

118         /// <summary>

119         /// 功能: 判断表是否存在

120         /// </summary>

121         /// <param name="tableName"></param>

122         /// <returns>存在不存在</returns>

123         public bool IsTableExist(string tableName)

124         {

125             using (SQLiteConnection connection = new SQLiteConnection(connStr))

126             {

127                 connection.Open();

128                 using (SQLiteCommand command = new SQLiteCommand(connection))

129                 {

130 

131                     command.CommandText = "SELECT COUNT(*) FROM sqlite_master where type='table' and name='" + tableName + "'";

132                     int tableCount = Convert.ToInt32(command.ExecuteNonQuery());

133                     if (Convert.ToInt32(command.ExecuteScalar()) == 0)

134                     {

135                         return false;

136                     }

137                     else

138                     {

139                         return true;

140                     }

141                 }

142             }

143         }

144         #endregion

145     }

146 }

你可能感兴趣的:(sqlite)