使用C#連結Ms SQL並且列出指定DB內的所有資料表名稱

使用C#連結Ms SQL並且列出指定DB內的所有資料表名稱

使用C#連結Ms SQL並且列出指定DB內的所有資料表名稱


 

資料來源:http://livin.tw/?p=555

http://stackoverflow.com/questions/3005095/can-i-get-name-of-all-tables-of-sql-server-database-in-c-sharp-application

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data.SqlClient;

using System.Data;

 

namespace CS_Console_MsSQL

{

    class Program

    {

        static
void Pause()

        {

            Console.Write(“Press any key to continue . . . “);

            Console.ReadKey(true);

        }

        static
void Main(string[]
args)

        {

            //http://livin.tw/?p=555

            //A@Gsg2SQLMeAw

            String
IP = “127.0.0.1”;

            String
DBName = “v7”;

            String
User = “root”;

            String
Password = “asd700502”;

            String
Connection = String.Format(“server={0}; database={1};uid={2};pwd={3}”,
IP, DBName, User, Password);

            SqlConnection
conn = new SqlConnection(Connection);

            conn.Open();

 

            //http://stackoverflow.com/questions/3005095/can-i-get-name-of-all-tables-of-sql-server-database-in-c-sharp-application

            DataTable
dt = conn.GetSchema(“Tables”);

            List<string> tables = new
List<string>();

            foreach
(DataRow row in
dt.Rows)

            {

                string
tablename = (string)row[2];

                tables.Add(tablename);

                Console.WriteLine(tablename);

            }

            /*

            //AGGXoaSQLuDO

            string sqlstr =
“select * from
MeAi”;

            SqlCommand cmd = new
SqlCommand(sqlstr, conn);

 

            SqlDataReader dr =
cmd.ExecuteReader();

 

            //ATGLDXMeA

            while (dr.Read())

            {

                String s =
dr[”
MeAai”].ToString();

            }

 

            //AD|Go?su

            cmd.Cancel();

            dr.Close();

            */

            Pause();

            conn.Close();

            conn.Dispose();

        }

    }

}

 

 

 


 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *