欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

c#获得目标服务器中所有数据库名、表名、列名的实现代码

时间:2022-02-22 10:57:16|栏目:.NET代码|点击:

复制代码 代码如下:

      /// <summary>
        /// 获得目标服务器所有数据库名
        /// </summary>
        /// <param name="serverName"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        public void getDataBaseNameList(string serverName, string userName, string password)
        {
            SQLDMO.Application sqlApplication = new SQLDMO.ApplicationClass();
            SQLDMO.SQLServer sqlServer = new SQLDMO.SQLServerClass();

            sqlServer.Connect(serverName, userName, password);          // 连接服务器

            foreach (SQLDMO.Database databBase in sqlServer.Databases)
            {
                if (databBase.Name != null)
                {
                    this.DataBaseTreeView.Nodes.Add(databBase.Name);

                    getDataBaseTableList(serverName, userName, password, databBase.Name);
                }
            }
        }


        /// <summary>
        /// 加载数据库中表
        /// </summary>
        /// <param name="serverName">服务器名</param>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="dataBaseName">数据库名</param>
        private void getDataBaseTableList(string serverName, string userName, string password, string dataBaseName)
        {
            SQLDMO.SQLServer Server = new SQLDMO.SQLServerClass();

            //连接到服务器
            Server.Connect(serverName, userName, password);

            //对所有的数据库遍历,获得指定数据库
            for (int i = 0; i < Server.Databases.Count; i++)
            {
                //判断当前数据库是否是指定数据库
                if (Server.Databases.Item(i + 1, "dbo").Name == dataBaseName)
                {
                    //获得指定数据库
                    SQLDMO._Database db = Server.Databases.Item(i + 1, "dbo");

                    //获得指定数据库中的所有表
                    for (int j = 0; j < db.Tables.Count; j++)
                    {
                        this.DataBaseTreeView.Nodes[i].Nodes.Add(db.Tables.Item(j + 1, "dbo").Name);
                    }
                }
            }
        }


    /// <summary>
        /// 获得表中所有列名
        /// </summary>
        /// <param name="serverName">服务器名</param>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="tableName">表名</param>
        /// <param name="dataBaseName">数据库名</param>
        /// <returns></returns>
        public string getRowListFromTable(string serverName, string userName, string password, string tableName, string dataBaseName)
        {
            string result = string.Empty;

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

            string commandString = string.Empty;
            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();
            dataAdapter.Fill(dataSet);

            DataTable dataTable = dataSet.Tables[0];

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

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

上一篇:ASP.NET中实现导出ppt文件数据的实例分享

栏    目:.NET代码

下一篇:.NET6 ConfigurationManager的实现及使用方式

本文标题:c#获得目标服务器中所有数据库名、表名、列名的实现代码

本文地址:http://www.codeinn.net/misctech/194193.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有