通过c#代码连接到mysql服务器

时间:2017-01-14 16:48:51

标签: c# mysql sql connection

我正在尝试使用c#代码连接到安装在vmware机器上的mysql服务器并执行查询,我不能这样做。 我在我的主机上测试了与uld文件的连接,这很有效。 我还测试了我在我的电脑上没有托管的sql server上的代码,这对我有用。

        private void button1_Click(object sender, EventArgs e)
    {
        connString = "SERVER=(Main Server Ip)\\SSER;PORT=3306;DATABASE=......;UID=.......;PASSWORD=......";
        try
        {
            conn = new MySqlConnection();
            conn.ConnectionString = connString;
            conn.Open();
            //    MessageBox.Show("connection success");


            string query = "SELECT * FROM dbo.Table_1";
            //Create Command
            MySqlCommand cmd = new MySqlCommand(query, conn);
            //Create a data reader and Execute the command
            MySqlDataReader dataReader = cmd.ExecuteReader();


            //Read the data and store them in the list
            while (dataReader.Read())
            {
                Console.WriteLine(dataReader["id"] + "");
                Console.WriteLine(dataReader["num"] + "");
            }
            conn.Close();
        }
        catch (MySql.Data.MySqlClient.MySqlException ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

0 个答案:

没有答案