Esempio n. 1
0
 public void CloseDb()
 {
     if (DbIsOpen())
     {
         MyConn.Close();
     }
     MyTools.WriteLogFile("MySqliteClass.CloseDb", "sucesss.");
 }
Esempio n. 2
0
        public bool ReadData(string SqlString, ref DataSet oDataSet, ref string sErrMsg)
        {
            bool bResult          = false;
            SQLiteDataAdapter da  = null;
            DataSet           odt = null;

            //string sToday = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            try
            {
                da = new SQLiteDataAdapter();
                da.SelectCommand = new SQLiteCommand(SqlString, MyConn);
                odt = new DataSet();
                da.Fill(odt);

                if (odt.Tables[0].Rows.Count > 0)
                {
                    oDataSet = odt.Copy();
                }
                else
                {
                    oDataSet = null;
                }
                MyErrMsg = "读数据成功。";
                bResult  = true;
            }
            catch (Exception ex)
            {
                MyErrMsg = "读出记录时发生错误:" + ex.Message;
                MyTools.WriteLogFile("MySqliteClass.ReadData = Err.", "sql = " + SqlString + ", err = " + ex.Message);
            }

            //Eend:
            if (odt != null)
            {
                odt.Dispose();
            }
            if (da != null)
            {
                da.Dispose();
            }
            sErrMsg = MyErrMsg;
            return(bResult);
        }
Esempio n. 3
0
 public bool OpenDb()
 {
     try
     {
         if (!DbIsOpen())
         {
             MyConn = new SQLiteConnection();
             MyConn.ConnectionString = "DataSource = " + MyDbName;
             MyConn.Open();
             MyErrMsg = "Open db succ.";
         }
     }
     catch (Exception ex)
     {
         MyErrMsg = "Open db fault = " + ex.Message;
         MyTools.WriteLogFile("MySqliteClass.OpenDb = Err.", ex.Message);
     }
     return(DbIsOpen());
 }
Esempio n. 4
0
        public bool WriteData(string SqlString, bool ReturnID, ref int ID, ref string sErrMsg)
        {
            bool             bResult = false;
            int              iJ;
            SQLiteCommand    cmd    = null;
            SQLiteDataReader myRead = null;

            try
            {
                cmd             = new SQLiteCommand();                    //定义OleDbCommand对象cmd
                cmd.Connection  = MyConn;
                cmd.CommandType = System.Data.CommandType.Text;           //指定SqlCommand类型
                cmd.CommandText = SqlString;

                iJ = cmd.ExecuteNonQuery();                                                 //执行
                if (iJ < 0)
                {
                    MyErrMsg = "写入记录时失败。";
                }
                else
                {
                    if (ReturnID)
                    {
                        if (iJ <= 0)
                        {
                            MyErrMsg = "写入记录时失败。";
                        }
                        else
                        {
                            cmd.CommandText = "select last_insert_rowid()";
                            myRead          = cmd.ExecuteReader();                        //执行
                            if (!myRead.Read())
                            {
                                sErrMsg = "插入新记录后读ID失败。";
                                ID      = -1;
                            }
                            else
                            {
                                ID       = myRead.GetInt32(0);
                                MyErrMsg = "写入记录成功。";
                            }
                            bResult = true;
                        }
                    }
                    else
                    {
                        bResult = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MyErrMsg = "写入记录时发生错误:" + ex.Message;
                MyTools.WriteLogFile("MySqliteClass.WriteData = Err.", "sql = " + SqlString + ", err = " + ex.Message);
            }

            if (myRead != null)
            {
                myRead.Dispose();
            }
            if (cmd != null)
            {
                cmd.Dispose();
            }
            sErrMsg = MyErrMsg;
            return(bResult);
        }