sq dll

16407 단어 dll
using System.Data;
  using System.Data.SQLite;
  using System.IO;
     
  namespace MyHelper.DataAccess
  {
       public class SQLiteHelper
       {
           private static string password = "***" // ***
           private static string dbFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly()
               .GetName().CodeBase) + "//***.db" // *** SQLite
           private static string connectString = string .Format( "Data Source =/"{}/"" , dbFilePath, password);
           private static SQLiteConnection myConnect = new SQLiteConnection(connectString);
     
           /**/ /// <summary>
           /// SQLite
           /// </summary>
           /// <returns> SQLite </returns>
           public static SQLiteConnection GetConnection()
           {
               return myConnect;
           }
     
           /**/ /// <summary>
           /// SQL ,
           /// </summary>
           /// <param name="commandString">SQL </param>
           /// <param name="parameters">SQL </param>
           /// <returns> </returns>
           public static int ExecuteNonQuery( string commandString, params SQLiteParameter[] parameters)
           {
               int result = ;
               using (SQLiteCommand command = new SQLiteCommand())
               {
                   PrepareCommand(command, null , commandString, parameters);
                   result = command.ExecuteNonQuery();
                   command.Parameters.Clear();
               }
               return result;
           }
     
           /**/ /// <summary>
           /// SQL ,
           /// </summary>
           /// <param name="transaction">SQL </param>
           /// <param name="commandString">SQL </param>
           /// <param name="parameters">SQL </param>
           /// <returns> </returns>
           public static int ExecuteNonQuery(SQLiteTransaction transaction, string commandString,
               params SQLiteParameter[] parameters)
           {
               int result = ;
               using (SQLiteCommand command = new SQLiteCommand())
               {
                   PrepareCommand(command, transaction, commandString, parameters);
                   result = command.ExecuteNonQuery();
                   command.Parameters.Clear();
               }
               return result;
           }
     
           /**/ /// <summary>
           /// , ,
           /// </summary>
           /// <param name="commandString">SQL </param>
           /// <param name="parameters">SQL </param>
           /// <returns> </returns>
           public static object ExecuteScalar( string commandString, params SQLiteParameter[] parameters)
           {
               object result;
               using (SQLiteCommand command = new SQLiteCommand())
               {
                   PrepareCommand(command, null , commandString, parameters);
                   result = command.ExecuteScalar();
               }
               return result;
           }
     
           /**/ /// <summary>
           /// SQL , DataReader
           /// </summary>
           /// <param name="commandString">SQL </param>
           /// <param name="parameters">SQL </param>
           /// <returns> DataReader</returns>
           public static SQLiteDataReader ExecuteReader( string commandString, params SQLiteParameter[] parameters)
           {
               SQLiteCommand command = new SQLiteCommand();
               try
               {
                   PrepareCommand(command, null , commandString, parameters);
                   SQLiteDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                   command.Parameters.Clear();
                   return reader;
               }
               catch
               {
                   throw ;
               }
           }
     
           /**/ /// <summary>
           /// Command , , , ,
           /// </summary>
           /// <param name="command">Command </param>
           /// <param name="transaction">transaction </param>
           /// <param name="commandString">SQL </param>
           /// <param name="parameters">SQL </param>
           private static void PrepareCommand(SQLiteCommand command, SQLiteTransaction transaction,
               string commandString, params SQLiteParameter[] parameters)
           {
               if (myConnect.State != ConnectionState.Open)
                   myConnect.Open();
     
               command.Connection = myConnect;
               command.CommandText = commandString;
     
               if (transaction != null )
                   command.Transaction = transaction;
               if (parameters != null && parameters.Length > )
               {
                   command.Parameters.AddRange(parameters);
               }
           }
       }
  }

좋은 웹페이지 즐겨찾기