ASP.Net可以连接到MySQL服务器,但我不能撤回数据?

时间:2016-05-10 16:36:42

标签: c# mysql asp.net networking phpmyadmin

我正在使用ASP.NET C#尝试连接到MySQL服务器以将数据拉回到Gridview中。我可以连接,但我无法撤回任何数据。它根本就不存在。当然,SQL服务器上有数据,但我似乎无法进入我的网站。有什么想法吗?

参考代码:

protected void Page_Load(object sender, EventArgs e){   

    MySqlConnection conn = new MySqlConnection();
    MySqlCommand comm = new MySqlCommand();
    MySqlDataAdapter adpater = new MySqlDataAdapter();
    DataTable data = new DataTable();
    string query;
    string connString = "Server=Server address; Database=sql8118918; Uid=my username; Pwd=my password; ";

    conn = new MySqlConnection();
    conn.ConnectionString = connString;       
        try
        {
           conn.Open();
           adpater = new MySqlDataAdapter();
           comm = new MySqlCommand();
           data = new DataTable();
           query = "SELECT * FROM products";

           comm.Connection = conn;
           comm.CommandText = query;

           adpater.SelectCommand = comm;
           adpater.Fill(data);
           GridView1.DataSource = data;
           logLabel.Text = ("Connection Successful !");
        }
        catch(Exception ex)
        {
            logLabel.Text = ex.ToString();
        }

就像我说我可以连接但没有数据被撤回。 我还尝试将数据放入一个字符串,然后在前端显示该字符串,但我得到的只是字符串中的这个?

System.Data.DataRowCollection

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

设置DataSource属性后应调用DataBind()方法。看看here.

GridView1.DataSource = data;
GridView1.DataBind();