Пример #1
0
        /*
         * 外部调用
         */

        #region [ 方法: 按条件查询 ]

        /// <summary>
        /// 查询Access数据库
        /// </summary>
        /// <param name="strCommand">命令</param>
        /// <param name="name0">连接字符串</param>
        /// <returns>数据集</returns>
        public DataTable DataSelete(string strCommand, string name0)
        {
            DataTable        dtSelect = new DataTable();
            OleDbConnection  connAcc  = null;
            OleDbDataAdapter myDA     = null;

            try
            {
                connAcc            = DAO.GetConn(name0);
                myDA               = new OleDbDataAdapter();
                myDA.SelectCommand = new OleDbCommand(strCommand, connAcc);
                myDA.Fill(dtSelect);
            }
            catch (Exception ex)
            {
                if (ex.Message.IndexOf("找不到输出表") != -1)
                {
                    if (ex.Message.IndexOf("NewData") != -1)
                    {
                        //插入New表
                        accImp.CreateNewTable(name0, ex.Message.Substring(ex.Message.IndexOf("'") + 1, 9));
                    }
                    else if (ex.Message.IndexOf("OrgData") != -1)
                    {
                        //插入Org表
                        accImp.CreateOrgTable(name0, ex.Message.Substring(ex.Message.IndexOf("'") + 1, 9));
                    }
                    else if (ex.Message.IndexOf("ConfigData") != -1)
                    {
                        //插入Config表
                        accImp.CreateConfigTable(name0, ex.Message.Substring(ex.Message.IndexOf("'") + 1, 12));
                    }
                }
                else if (ex.Message.IndexOf("操作必须使用一个可更新的查询") != -1)
                {
                    try
                    {
                        accImp.CancelFileReadOnly(connAcc.DataSource);
                    }
                    catch { }
                }
                else if (ex.Message.IndexOf("找不到文件") != -1)
                {
                    try
                    {
                        accImp.CopyMDB(connAcc.DataSource);
                    }
                    catch { }
                }
                else if (ex.Message.IndexOf("当前被锁定") != -1)
                {
                    if (ErrorMessage != null)
                    {
                        ErrorMessage(6020006, ex.StackTrace, "[InsertAccess:InsertData]", ex.Message + Thread.CurrentThread.Name);
                    }
                    Thread.Sleep(100);
                }
                else if (ex.Message.IndexOf("由于您和其他用户试图同时改变同一数据") != -1)
                {
                    if (connAcc.State != ConnectionState.Closed)
                    {
                        connAcc.Close();
                        connAcc.Dispose();
                        connAcc = null;
                    }
                    try
                    {
                        string strfilenameTemp = name0.Replace(".mdb", ".ldb");
                        if (File.Exists(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + strfilenameTemp))
                        {
                            File.Delete(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + strfilenameTemp);
                        }
                        File.Delete(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + name0);
                    }
                    catch { }
                    Thread.Sleep(100);
                }
                else if (ex.Message.IndexOf("不可识别的数据库格式") != -1)
                {
                    //if (ErrorMessage != null)
                    //{
                    //    ErrorMessage(6020006, ex.StackTrace, "[InsertAccess:InsertData]", ex.Message + Thread.CurrentThread.Name);
                    //}
                    if (connAcc.State != ConnectionState.Closed)
                    {
                        connAcc.Close();
                        connAcc.Dispose();
                        connAcc = null;
                    }
                    try
                    {
                        string strfilenameTemp = name0.Replace(".mdb", ".ldb");
                        if (File.Exists(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + strfilenameTemp))
                        {
                            File.Delete(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + strfilenameTemp);
                        }
                        File.Delete(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + name0);
                    }
                    catch { }
                    //catch (Exception ee)
                    //{
                    //    string s = ee.Message;
                    //}
                    Thread.Sleep(100);
                }

                if (ErrorMessage != null)
                {
                    ErrorMessage(6020011, ex.StackTrace, "[SelectAccess:DataSelete]", ex.Message);
                }
            }
            finally
            {
                if (myDA != null)
                {
                    myDA.Dispose();
                    myDA = null;
                }
                if (connAcc != null)
                {
                    connAcc.Dispose();
                    connAcc = null;
                }
            }

            return(dtSelect);
        }
Пример #2
0
        /// <summary>
        /// 向表中插入数据(公共方法)
        /// </summary>
        /// <param name="dbCommand">Command对象</param>
        /// <param name="strCreateInfo"></param>
        /// <param name="dataStream"></param>
        /// <returns>操作成功返回True</returns>
        private bool InsertData(int insertType, OleDbCommand dbCommand, string strCreateInfo, byte[] dataStream)
        {
            bool     falg = true;
            DateTime dt   = new DateTime(2000, 1, 1, 0, 0, 0);

            try
            {
                string newTime = strCreateInfo.Substring(0, 4) + "." + strCreateInfo.Substring(4, 2) + "." +
                                 strCreateInfo.Substring(6, 2);
                dt = Convert.ToDateTime(newTime);
            }
            catch
            {
                return(true);
            }
            if (dt.Equals(new DateTime(2000, 1, 1, 0, 0, 0)))
            {
                return(true);
            }
            string filename;

            filename = dt.ToString("yyyy-MM-dd") + ".mdb";
            if (insertType != 0)
            {
                filename = "Config\\config" + filename;
            }
            OleDbConnection conn = null; // 获取有效的连接

            try
            {
                conn = DAO.GetConn(filename); // 获取有效的连接
                dbCommand.Connection = conn;
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                dbCommand.ExecuteNonQuery();
                return(true);
            }
            catch (Exception ex)
            {
                if (Thread.CurrentThread.Name == "DataSave")
                {
                    Thread.Sleep(100);
                }
                if (ex.Message.IndexOf("找不到输出表") != -1)
                {
                    try
                    {
                        if (ex.Message.IndexOf("NewData") != -1)
                        {
                            //插入New表
                            accImp.CreateNewTable(filename, ex.Message.Substring(ex.Message.IndexOf("'") + 1, 9));
                        }
                        else if (ex.Message.IndexOf("OrgData") != -1)
                        {
                            //插入Org表
                            accImp.CreateOrgTable(filename, ex.Message.Substring(ex.Message.IndexOf("'") + 1, 9));
                        }
                        else if (ex.Message.IndexOf("ConfigData") != -1)
                        {
                            //插入Config表
                            accImp.CreateConfigTable(filename, ex.Message.Substring(ex.Message.IndexOf("'") + 1, 12));
                        }
                    }
                    catch { }
                    falg = false;
                }
                else if (ex.Message.IndexOf("操作必须使用一个可更新的查询") != -1)
                {
                    try
                    {
                        accImp.CancelFileReadOnly(conn.DataSource);
                    }
                    catch { }
                    falg = false;
                }
                else if (ex.Message.IndexOf("找不到文件") != -1)
                {
                    try
                    {
                        accImp.CopyMDB(conn.DataSource);
                    }
                    catch { }
                    falg = false;
                }
                else if (ex.Message.IndexOf("创建重复的值") != -1)
                {
                    //if (ErrorMessage != null)
                    //{
                    //    ErrorMessage(6020006, ex.StackTrace, "[InsertAccess:InsertData]", "有重复的值出现" + dataStream[0].ToString() + "." + dataStream[1].ToString() + "." + dataStream[2].ToString() + "." + strCreateInfo);
                    //}
                    falg = true;
                    //Thread.Sleep(80);
                }
                else if (ex.Message.IndexOf("当前被锁定") != -1)
                {
                    if (ErrorMessage != null)
                    {
                        ErrorMessage(6020006, ex.StackTrace, "[InsertAccess:InsertData]", ex.Message + Thread.CurrentThread.Name);
                    }
                    falg = false;
                    Thread.Sleep(100);
                }
                else if (ex.Message.IndexOf("由于您和其他用户试图同时改变同一数据") != -1)
                {
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                        conn.Dispose();
                        conn = null;
                    }
                    try
                    {
                        string strfilenameTemp = filename.Replace(".mdb", ".ldb");
                        if (File.Exists(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + strfilenameTemp))
                        {
                            File.Delete(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + strfilenameTemp);
                        }
                        File.Delete(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + filename);
                    }
                    catch { }
                    Thread.Sleep(100);
                    falg = false;
                }
                else if (ex.Message.IndexOf("不可识别的数据库格式") != -1)
                {
                    //if (ErrorMessage != null)
                    //{
                    //    ErrorMessage(6020006, ex.StackTrace, "[InsertAccess:InsertData]", ex.Message + Thread.CurrentThread.Name);
                    //}
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                        conn.Dispose();
                        conn = null;
                    }
                    try
                    {
                        string strfilenameTemp = filename.Replace(".mdb", ".ldb");
                        if (File.Exists(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + strfilenameTemp))
                        {
                            File.Delete(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + strfilenameTemp);
                        }
                        File.Delete(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + filename);
                    }
                    catch {}
                    //catch(Exception ee)
                    //{
                    //    string s = ee.Message;
                    //}
                    Thread.Sleep(100);
                    falg = false;
                }
                else
                {
                    if (ErrorMessage != null)
                    {
                        ErrorMessage(6020006, ex.StackTrace, "[InsertAccess:InsertData]", ex.Message + Thread.CurrentThread.Name);
                    }
                    Thread.Sleep(100);
                    falg = false;
                }
            }
            finally
            {
                if (conn != null)
                {
                    conn.Dispose();
                    conn = null;
                }
            }
            return(falg);
        }