sql server tablo listesi Etiketli Yazılar

C# – SQL Server Table listesini Datatable olarak alma

       public static DataTable GetDatabaseTables(string sConnection, string sDatabase)
        {
            SqlConnection connection = null;
            DataTable dt = null;
            try
            {
                connection = GetConnection(sConnection, sDatabase);
                if (connection == null)
                    return dt;
                SqlCommand cmd = new SqlCommand("sp_tables", connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@table_type", "'TABLE'");

                DataSet dset = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dset);
                if ((dset != null) && (dset.Tables.Count > 0))
                    dt = dset.Tables[0];
            }
            catch (Exception ex)
            {
                throw new Exception("GetDatabaseTables error: " + ex.Message);
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                    connection.Dispose();
                }
            }
            return dt;
        }