数据库连接不能正常工作

时间:2016-09-13 12:48:45

标签: c# mysql asp.net database phpmyadmin

我无法连接到我的数据库。 db info:dbname:users,dbtable;用户。

我一直收到错误,并且不知道如何修复它。

我得到的错误是:

  

我将在这里输入一个编辑。

它应该工作,因为我添加了所有深刻的引用(我认为?)并且在配置数据库时有一个有效的连接。

这是我的代码:

(初学者代码)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

namespace hpjct
{

public partial class _Default : Page
{
    private string conn;
    MySqlConnection connect;


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
        string user = username.Text;
        string pass = password.Text;

        if (user == "" || pass == "")
        {
            lbmsg.Text = "Please fill in all information";
            lbmsg.ForeColor = System.Drawing.Color.Red;
            return;
        }
        bool r = validate_login(user, pass);

        if (r)
        {
            lbmsg.Text = "Correct";
        }
        else
        {
            lbmsg.Text = "False";
        }
    }

    private void db_connection()
    {
        try
        {
            conn = "Server=localost;Database=users;Uid=root;Pwd=;";
            connect = new MySqlConnection(conn);
            connect.Open();
        }
        catch (MySqlException e)
        {
            throw;
        }
    }

    private bool validate_login(string user, string pass)
    {
        db_connection();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "SELECT * FROM `user` WHERE username=@username AND password=@password ";
        cmd.Parameters.AddWithValue("@username", user);
        cmd.Parameters.AddWithValue("@password", pass);
        cmd.Connection = connect;
        MySqlDataReader login = cmd.ExecuteReader();
        if (login.Read())
        {
            connect.Close();
            return true;
        }
        else
        {
            connect.Close();
            return false;
        }
    }

    private void submit_Click(object sender, EventArgs e)
    {

    }
}
}
提前谢谢!

1 个答案:

答案 0 :(得分:0)

修正了它!只需使用:

"Server=myServerAddress;Database=myDataBase;Uid=myUsername;P‌​wd=myPassword;"