C# 利用mysql.data 在mysql中创建数据库及数据表

C# 利用mysql.data 在mysql中创建数据库及数据表

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using MySql.Data.MySqlClient;



namespace CA_Mysql

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("1.创建数据表吗?");

            Console.ReadKey();

            NewDatatable("localhost", "root", "pwd", "newtest1", "newtb2");

            Console.ReadLine();

        }



        /// <summary>

        /// 创建新的数据库

        /// </summary>

        static void NewDatabase(string dbSource ,string dbUid,string dbPwd,string dbName)

        {

            //创建连接字符串con

            MySqlConnection con = new MySqlConnection("Data Source=" + dbSource + ";Persist Security Info=yes;UserId=" + dbUid + "; PWD="+dbPwd+";");

            //创建数据库的执行语句

            MySqlCommand cmd = new MySqlCommand("CREATE DATABASE " + dbName, con);

            con.Open();

            //执行语句

            int res = cmd.ExecuteNonQuery();

            con.Close();

        }



        /// <summary>

        /// 创建数据表

        /// </summary>

        static void NewDatatable(string dbSource, string dbUid, string dbPwd, string dbName,string tbName)

        {

            //创建连接字符串con

            MySqlConnection con = new MySqlConnection("Data Source=" + dbSource + ";Persist Security Info=yes;UserId=" + dbUid + "; PWD=" + dbPwd + ";");

            string newTableCMD = "USE " + dbName + "; CREATE TABLE `TESTTABLE3` (`Id_` int(11) NOT NULL AUTO_INCREMENT,`NAME` VARCHAR(32) NOT NULL,PRIMARY KEY(`Id_`));";

            MySqlCommand cmd = new MySqlCommand(newTableCMD,con);

            con.Open();

            //MySqlCommand cmdUseDB = new MySqlCommand("USE " + dbName, con);

            //cmdUseDB.ExecuteNonQuery();

            int res = cmd.ExecuteNonQuery();

            con.Close();

        }



    }

}

 

你可能感兴趣的:(mysql)