SoFunction
Updated on 2025-03-07

c# Obtain the implementation code of all database names, table names and column names in the target server


/// <summary>
/// Obtain all database names of the target server
        /// </summary>
        /// <param name="serverName"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        public void getDataBaseNameList(string serverName, string userName, string password)
        {
            sqlApplication = new ();
            sqlServer = new ();

(serverName, userName, password);                                                                                                                         �

            foreach ( databBase in )
            {
                if ( != null)
                {
                    ();

                    getDataBaseTableList(serverName, userName, password, );
                }
            }
        }


        /// <summary>
/// Load the table in the database
        /// </summary>
/// <param name="serverName">Server name</param>
/// <param name="userName">username</param>
/// <param name="password">Password</param>
/// <param name="dataBaseName">Database name</param>
        private void getDataBaseTableList(string serverName, string userName, string password, string dataBaseName)
        {
            Server = new ();

//Connect to the server
            (serverName, userName, password);

//Travel all databases and obtain the specified database
            for (int i = 0; i < ; i++)
            {
//Determine whether the current database is a specified database
                if ((i + 1, "dbo").Name == dataBaseName)
                {
//Get the specified database
                    SQLDMO._Database db = (i + 1, "dbo");

//Get all tables in the specified database
                    for (int j = 0; j < ; j++)
                    {
                        [i].((j + 1, "dbo").Name);
                    }
                }
            }
        }


    /// <summary>
/// Get all column names in the table
        /// </summary>
/// <param name="serverName">Server name</param>
/// <param name="userName">username</param>
/// <param name="password">Password</param>
/// <param name="tableName">table name</param>
/// <param name="dataBaseName">Database name</param>
        /// <returns></returns>
        public string getRowListFromTable(string serverName, string userName, string password, string tableName, string dataBaseName)
        {
            string result = ;

            string connectionString = ;
            connectionString += "server=" + serverName;
            connectionString += ";Pwd=" + password;
            connectionString += ";UID=" + userName;
            connectionString += ";Database=" + dataBaseName;

            string commandString = ;
            commandString += "select   name   from   syscolumns   where   id=object_id('";
            commandString += tableName;
            commandString += "')";
           

            SqlConnection sqlConnection = new SqlConnection(connectionString);
            SqlCommand sqlCommand = new SqlCommand(commandString, sqlConnection);

            SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, sqlConnection);

            DataSet dataSet = new DataSet();
            (dataSet);

            DataTable dataTable = [0];

            // DataTable dataTable = ("Tables");
            foreach (DataRow row in )
            {
                result += row[0].ToString() + "-";
            }

            if (result != null)
            {
                return result; 
            }
            else
            {
                return "0";
            }
        }