程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#取得目的辦事器中一切數據庫名、表名、列名的完成代碼

c#取得目的辦事器中一切數據庫名、表名、列名的完成代碼

編輯:C#入門知識

c#取得目的辦事器中一切數據庫名、表名、列名的完成代碼。本站提示廣大學習愛好者:(c#取得目的辦事器中一切數據庫名、表名、列名的完成代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是c#取得目的辦事器中一切數據庫名、表名、列名的完成代碼正文



      /// <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";
            }
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved