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

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

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


 

資料來源: http://roboardgod.blogspot.tw/2013/08/cmysql.html

http://dev.mysql.com/downloads/connector/net/6.6.html

http://stackoverflow.com/questions/14251639/how-to-get-liststring-of-table-names-from-mysql-db

https://www.connectionstrings.com/mysql/

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using MySql.Data.MySqlClient;

namespace CS_Console_MySQL

{

   
class Program

   
{

        static void Pause()

        {

 

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

 

            Console.ReadKey(true);

 

        }

        static void Main(string[] args)

        {

            string dbHost =
“127.0.0.1”;//
資料庫位址

            string dbUser =
“root”;//
資料庫使用者帳號

            string dbPass =
“usbw”;//
資料庫使用者密碼

            string dbName =
“invoices”;//
資料庫名稱

 

           
//http://roboardgod.blogspot.tw/2013/08/cmysql.html

            string connStr =
“server=” + dbHost + “;uid=” + dbUser + “;pwd=”
+ dbPass + “;database=” + dbName + “;Port=3307”;

            MySqlConnection conn = new
MySqlConnection(connStr);

            /*

            MySqlCommand command = conn.CreateCommand();

            */

            conn.Open();

 

           
//http://stackoverflow.com/questions/14251639/how-to-get-liststring-of-table-names-from-mysql-db

            string query = “show tables
from ” + dbName;

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

            MySqlCommand cmdName = new
MySqlCommand(query, conn);

            MySqlDataReader reader =
cmdName.ExecuteReader();

            while (reader.Read())

            {

               
QueryResult.Add(reader.GetString(0));

               
Console.WriteLine(reader.GetString(0));

            }

            reader.Close();

            /*

            String account;

            String password;

            int level;

            for (int i = 0; i < 10; i++)

            {

                account = “account” +
i.ToString();

                password =
“password” + i.ToString();

                level = i * 10;

                command.CommandText =
“Insert into member(account,password,level) values(‘” + account +
“‘,'” + password + “‘,” + level + “)”;

                command.ExecuteNonQuery();

            }

            Console.ReadLine();

            */

            conn.Close();

            Pause();

        }

   
}

}

 


 


發表迴響

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