错误“无法连接到任何指定的MySQL主机。”

时间:2017-07-23 09:54:35

标签: c# mysql

我是c#的新手。我想连接到我的数据库。如果我使用地址“localhost”这样做,它工作正常。如果我使用我的Web服务器IP它不起作用和错误

  

无法连接到任何指定的MySQL主机。

我的问题是什么?

namespace MySqlCRUD
{
public partial class Form1 : Form
{

    String connectionString = "server=84.200.7.136;uid=root;" +
    "pwd=;database=bookdb;";


    int AcapWatchID = 0;
    public Form1()
    {
        InitializeComponent();
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        using (MySqlConnection mysqlCon = new MySqlConnection(connectionString))
        {
            mysqlCon.Open();
            MySqlCommand mySqlCmd = new MySqlCommand("BookAddOrEdit",mysqlCon);
            mySqlCmd.CommandType = CommandType.StoredProcedure;
            mySqlCmd.Parameters.AddWithValue("_AcapWatchID", AcapWatchID);
            mySqlCmd.Parameters.AddWithValue("_Artist", txtArtist.Text.Trim());
            mySqlCmd.Parameters.AddWithValue("_Albumname", txtAlbumname.Text.Trim());
            mySqlCmd.Parameters.AddWithValue("_Download1", txtDownload1.Text.Trim());
            mySqlCmd.Parameters.AddWithValue("_Download2", txtDownload2.Text.Trim());
            mySqlCmd.Parameters.AddWithValue("_Verlinkt",  txtVerlinkt.Text.Trim());
            mySqlCmd.ExecuteNonQuery();
            MessageBox.Show("[ACAPWatch] Album wurde erfolgreich hinzugefügt!");
            Clear();
            GridFill();
        }
    }

    void GridFill()
    {
        using (MySqlConnection mysqlCon = new MySqlConnection(connectionString))
        {
            mysqlCon.Open();
            MySqlDataAdapter sqlDa = new MySqlDataAdapter("BookViewAll",mysqlCon);
            sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dtblBook = new DataTable();
            sqlDa.Fill(dtblBook);
            dgvBook.DataSource = dtblBook;
            dgvBook.Columns[0].Visible = false;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Clear();
        GridFill();
    }

    void Clear()
    {
        txtArtist.Text = txtAlbumname.Text = txtDownload1.Text = txtDownload2.Text = txtVerlinkt.Text = txtSearch.Text = "";
        AcapWatchID = 0;
        btnSave.Text = "Speichern";
        btnDelete.Enabled = false;
    }

    private void dgvBook_DoubleClick(object sender, EventArgs e)
    {
        if (dgvBook.CurrentRow.Index != -1)
        {
            txtArtist.Text = dgvBook.CurrentRow.Cells[1].Value.ToString();
            txtAlbumname.Text = dgvBook.CurrentRow.Cells[2].Value.ToString();
            txtDownload1.Text = dgvBook.CurrentRow.Cells[3].Value.ToString();
            txtDownload2.Text = dgvBook.CurrentRow.Cells[4].Value.ToString();
            txtVerlinkt.Text = dgvBook.CurrentRow.Cells[5].Value.ToString();
            AcapWatchID = Convert.ToInt32(dgvBook.CurrentRow.Cells[0].Value.ToString());
            btnSave.Text = "Löschen";
            btnDelete.Enabled = Enabled;
        }
    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
        using (MySqlConnection mysqlCon = new MySqlConnection(connectionString))
        {
            mysqlCon.Open();
            MySqlDataAdapter sqlDa = new MySqlDataAdapter("BookSearchByValue", mysqlCon);
            sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
            sqlDa.SelectCommand.Parameters.AddWithValue("_SearchValue",txtSearch.Text);
            DataTable dtblBook = new DataTable();
            sqlDa.Fill(dtblBook);
            dgvBook.DataSource = dtblBook;
            dgvBook.Columns[0].Visible = false;
        }
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        Clear();
    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
        using (MySqlConnection mysqlCon = new MySqlConnection(connectionString))
        {
            mysqlCon.Open();
            MySqlCommand mySqlCmd = new MySqlCommand("BookDeleteByID", mysqlCon);
            mySqlCmd.CommandType = CommandType.StoredProcedure;
            mySqlCmd.Parameters.AddWithValue("_AcapWatchID", AcapWatchID);
            mySqlCmd.ExecuteNonQuery();
            MessageBox.Show("[ACAPWatch!] Album wurde aus der Datenbank entfernt!");
            Clear();
            GridFill();
        }
    }

}
}

2 个答案:

答案 0 :(得分:0)

您的MySQL是否与WebServer在同一台服务器上运行? 是的,在同一台服务器上运行。

如果是,是否在默认端口(3306)?您的WebServers防火墙是否允许MySQL端口上的流量?

是,在默认端口上运行,防火墙允许此端口上的流量;)

答案 1 :(得分:0)

我刚刚尝试使用你的连接字符串进行连接并得到了一个不同的错误(由于缺少密码而被拒绝),这正是我所期望的。
发布问题以包含真正的连接字符串(假设您提供的IP地址等是真实的)通常不是一个好主意。您正在暴露您的数据库以进行攻击!