美文网首页技术初心
C#判断表是否存在MySQL、PostgreSQL、SqlSer

C#判断表是否存在MySQL、PostgreSQL、SqlSer

作者: triplestudio | 来源:发表于2020-07-01 11:16 被阅读0次

    MySQL

    public static bool IsTableExists(string connectionString, string tblName)
    {
        string sql = "SELECT table_name FROM information_schema.TABLES WHERE table_name =@tableName";
        string result = MySQLHelper.ExecuteScalar(connectionString,
            System.Data.CommandType.Text, sql,
            new MySqlParameter("tableName", tblName)) as string;
        return String.IsNullOrEmpty(result) ? false : true;
    }
    

    PostgreSQL

    public static bool IsTableExists(string connectionString, string tblName)
    {
        string sql = "select to_regclass(@tableName)";
        string result = PostgreSQLHelper.ExecuteScalar(connectionString, 
               System.Data.CommandType.Text, sql, 
               new NpgsqlParameter("tableName", tblName)) as string;
        return String.IsNullOrEmpty(result) ? false : true;
    }
    

    SqlServer

    public static bool IsTableExists(string connectionString, string tblName)
    {
        string sql = "if object_id(@tableName) is not null select 'ok' else select ''";
        string result = SqlHelper.ExecuteScalar(connectionString, 
            System.Data.CommandType.Text, sql, 
            new SqlParameter("tableName", tblName)) as string;
        return String.IsNullOrEmpty(result) ? false : true;
    }
    

    相关文章

      网友评论

        本文标题:C#判断表是否存在MySQL、PostgreSQL、SqlSer

        本文链接:https://www.haomeiwen.com/subject/xwhcqktx.html