示例#1
0
 /// <summary>
 /// 创建数据库连接
 /// </summary>
 /// <param name="strMDBName">数据库名称,例如“2008-1-23.mdb”</param>
 /// <returns></returns>
 public OleDbConnection SetConnect(string strMDBName)
 {
     //string strDBPath = System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + strMDBName;  //数据库路径
     //OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + strDBPath + ";Jet OLEDB:Database Password=shkj;");
     //return con;
     //accessbaseconn = DAO.GetConn(strMDBName);
     //return accessbaseconn;
     return(DAO.GetConn(strMDBName));
 }
示例#2
0
        /// <summary>
        /// 向数据库中插入表
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="strCommand">创建命令字符串</param>
        /// <returns>创建成功返回True</returns>
        private bool CreteTableBase(string filename, string strCommand)
        {
            OleDbConnection conn = DAO.GetConn(filename);
            OleDbCommand    cmd  = new OleDbCommand(strCommand, conn);
            bool            falg = true;

            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                if (ErrorMessage != null)
                {
                    ErrorMessage(6020006, ex.StackTrace, "[AccessImport:CreteTableBase]", ex.Message);
                }
                falg = false;
            }
            finally
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }
                if (conn != null)
                {
                    conn.Dispose();
                    conn = null;
                }
            }
            return(falg);
        }
示例#3
0
        /// <summary>
        /// 执行command命令(删除/插入/修改)
        /// </summary>
        /// <param name="strCommand">command命令字符</param>
        /// <param name="filename">数据库连接</param>
        /// <returns>成功返回true</returns>
        public bool ExcuteCommand(string strCommand, string filename)
        {
            bool            falg    = true;
            OleDbConnection conn    = null;
            OleDbCommand    command = null;

            try
            {
                conn    = DAO.GetConn(filename);
                command = new OleDbCommand(strCommand, conn);
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                int num = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                if (Thread.CurrentThread.Name == "DataSave")
                {
                    Thread.Sleep(100);
                }
                if (ex.Message.IndexOf("由于您和其他用户试图同时改变同一数据") != -1)
                {
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                        conn.Dispose();
                        conn = null;
                    }
                    try
                    {
                        string strfilenameTemp = System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + filename.Replace(".mdb", ".ldb");
                        if (File.Exists(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + filename))
                        {
                            File.Delete(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + filename);
                        }
                        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(6020012, ex.StackTrace, "[UpDataAccess:ExcuteCommand]", ex.Message + Thread.CurrentThread.Name);
                    //}
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                        conn.Dispose();
                        conn = null;
                    }
                    try
                    {
                        string strfilenameTemp = System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + filename.Replace(".mdb", ".ldb");
                        if (File.Exists(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + filename))
                        {
                            File.Delete(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + filename);
                        }
                        File.Delete(System.Windows.Forms.Application.StartupPath.ToString() + @"\AccessDB\" + filename);
                    }
                    catch { }
                    Thread.Sleep(100);
                    falg = false;
                }
                else
                {
                    if (ErrorMessage != null)
                    {
                        ErrorMessage(6020012, ex.StackTrace, "[UpDataAccess:ExcuteCommand]", Thread.CurrentThread.Name + ex.Message + strCommand);
                    }
                    falg = false;
                }
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                    command = null;
                }
                if (conn != null)
                {
                    conn.Dispose();
                    conn = null;
                }
            }
            return(falg);
        }
示例#4
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);
        }
示例#5
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);
        }