Пример #1
0
        public bool DbTableExists(SQLTable tbl, ref string  csError)
        {
            //string strTableNameAndSchema = " SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '"+tbl.TableName+"'";
            //string strCheckTable =   m_strSQLUseDatabase + String.Format(
            //      "IF OBJECT_ID('{0}', 'U') IS NOT NULL SELECT 'true' ELSE SELECT 'false' ",strTableNameAndSchema
            //      );
            string strCheckTable = null;
            switch (m_con.DBType)
            {
                case DBConnection.eDBType.MYSQL:
                    strCheckTable = "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '" + this.m_con.DataBase+"' AND table_name = '" + tbl.TableName + "';";
                    break;
                case DBConnection.eDBType.MSSQL:
                    strCheckTable = "\nUSE " + this.m_con.DataBase + "\n IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_NAME='" + tbl.TableName + "') SELECT 1 AS res ELSE SELECT 0 AS res";
                    break;
                case DBConnection.eDBType.SQLITE:
                    strCheckTable = "SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = '" + tbl.TableName + "'";
                    break;
            }
            StringBuilder strB_CheckTable = new StringBuilder(strCheckTable);
            int i = 0;
            Object ObjRet = new Object();
            if (this.m_con.ExecuteQuerySQL(strB_CheckTable, null, ref i, ref ObjRet, ref csError, tbl.TableName))
            {
                if (ObjRet.GetType() == typeof(int))
                {
                    i =(int)ObjRet;
                    if (i == 1)
                    {
                        return true;
                    }
                    else
                    {
                        csError = lngConn.s_Error.s + ": " + lngConn.sTableIsMissing.s + ":" + tbl.TableName;
                        return false;
                    }
                }
                else if (ObjRet.GetType() == typeof(long))
                {
                    long l;
                    l = (long)ObjRet;
                    if (l == 1)
                    {
                        return true;
                    }
                    else
                    {
                        if (tbl.TableName.Equals("RemoteUpdate"))
                        {
                            List<string> UniqueConstraintNameList = new List<string>();
                            string sql_DBm = "";
                            string sql_create = tbl.SQLcmd_CreateTable(this, UniqueConstraintNameList, ref sql_DBm,null);
                            string sql_create_All = sql_DBm + sql_create;
                            object oResult=null;
                            if (m_con.ExecuteNonQuerySQL(sql_create_All, null, ref oResult, ref csError))
                            {
                                return true;
                            }
                            else
                            {
                                return false;
                            }

                        }
                        else
                        {
                            csError = lngConn.s_Error.s + ": " + lngConn.sTableIsMissing.s + ":" + tbl.TableName;
                            return false;
                        }
                    }
                }
            }
            return false;
        }