3

我正在尝试创建一个连接到 MySQL 数据库的登录表单。我安装了插入表单中的 sql 连接器,但是当我尝试连接时,出现无法连接到任何指定 MySQL 主机的错误。这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ECBSRecruitmentAgencySoftware
{
    public partial class LogIn : Form
    {
             public LogIn()
        {
            InitializeComponent();
        }

             public bool tryLogin(string username, string password)
             {
                 MySqlConnection con = new MySqlConnection("host=think-tek.net;user=ctutorial;password=chang3d;database=ctutorial_logintest;");
                 MySqlCommand cmd = new MySqlCommand("Select * FROM login WHERE user_name = `" + username + "` AND user_pass = `" + password + "`;");
                 cmd.Connection = con;
                 con.Open();
                 MySqlDataReader reader = cmd.ExecuteReader();
                 if (reader.Read() != false)
                 {
                     if (reader.IsDBNull(0) == true)
                     {
                         cmd.Connection.Close();
                         reader.Dispose();
                         cmd.Dispose();
                         return false;
                     }
                     else
                     {
                         cmd.Connection.Close();
                         reader.Dispose();
                         cmd.Dispose();
                         return true;
                     }
                 }
                 else 
                 {
                     return false;
                 }
             }
        private void button1_Click(object sender, EventArgs e)
        {

            if (tryLogin(textBox1.Text, textBox2.Text) == true)
            {
                MainScreen F2 = new MainScreen();
                F2.Show();
                this.Hide();
            }

             else MessageBox.Show("Wrong details!");

        }




}
}
4

6 回答 6

5

该站点在连接字符串方面非常有用。您的连接字符串似乎无效。

另外:确保您的用户具有适当的访问权限。许多托管服务提供商仅允许从 localhost 进行访问。您可能必须请求他们允许您的用户进行远程访问。

于 2012-05-16T13:07:24.980 回答
1

我的 MySQL 连接字符串是:

string mySqlConn = "server=localhost;user=username;database=databasename;port=3306;password=password;";

您的连接字符串会引发什么异常?应该有一个错误号。

于 2012-05-16T12:48:53.237 回答
0

尝试示例中给定格式的连接字符串:

public bool tryLogin(string username, string password)
             {
                 MySqlConnection con = new MySqlConnection("SERVER=localhost;" +
                    "DATABASE=mydatabase;" +
                    "UID=testuser;" +
                    "PASSWORD=testpassword;");
                 MySqlCommand cmd = new MySqlCommand("Select * FROM login WHERE user_name = `" + username + "` AND user_pass = `" + password + "`;");
                 cmd.Connection = con;
                 con.Open();
                 MySqlDataReader reader = cmd.ExecuteReader();
                 if (reader.Read() != false)
                 {
                     if (reader.IsDBNull(0) == true)
                     {
                         cmd.Connection.Close();
                         reader.Dispose();
                         cmd.Dispose();
                         return false;
                     }
                     else
                     {
                         cmd.Connection.Close();
                         reader.Dispose();
                         cmd.Dispose();
                         return true;
                     }
                 }
                 else 
                 {
                     return false;
                 }
             }          
于 2012-05-16T12:46:07.083 回答
0

我有同样的问题,错误是在连接字符串中!不知何故,我向两个不同的位置声明了两次。一次到我的本地主机,第二次到服务器机器数据库。当我在客户端机器上发布项目时,无法连接到本地主机(我的电脑是什么)。:-) 我想知道如何得到这个:“无法连接到任何指定的 mysql 主机”

这就是人如何让他的生活复杂好几天!:-)

当然,我能够连接到服务器机器上的数据库。

所以,举个例子,这是我的问题。而且,解决方案是一次性向它声明一个数据库!!!

而且,我想分享我工作得很好的连接字符串:

cs = @"服务器=xxx.xxx.xxx.xxx;uid=xxxx;密码=xxxxxxxxx;数据库=xxxxx;端口=3306";

于 2012-09-01T10:41:58.113 回答
0

你的字符串连接是正确的,没有错误,但首先在本地服务器上检查它这是异常只会引发你的xampp没有运行,首先启动xampp去浏览器并输入localhost,如果它工作你会看到xampp菜单如果它打开localhost然后尝试连接您的服务器

string connection = "server=localhost;database=student_record_database;user=root;password=;";
            MySqlConnection con = new MySqlConnection(connection);
于 2015-02-07T11:14:20.797 回答
0
public bool tryLogin(string username, string password)
{
    MySqlConnection con = new MySqlConnection("SERVER=xx.xx.xx.xx;DATABASE=dbname;UID=user;PASSWORD=password;CheckParameters=False;");
    MySqlCommand cmd = new MySqlCommand("Select * FROM login WHERE user_name = `" + username + "` AND user_pass = `" + password + "`;");
    cmd.Connection = con;
    con.Open();
    MySqlDataReader reader = cmd.ExecuteReader();
    if (reader.Read() != false)
    {
        if (reader.IsDBNull(0) == true)
        {
            cmd.Connection.Close();
            reader.Dispose();
            cmd.Dispose();
            return false;
        }
        else
        {
            cmd.Connection.Close();
            reader.Dispose();
            cmd.Dispose();
            return true;
        }
    }
    else 
    {
        return false;
    }
}
于 2017-08-21T10:51:14.663 回答