0

我试图连接到数据库 MySql 但出现了这个错误

FileNotFoundException 未处理

在行中adapter.Fill

FileNotFoundException 未处理无法加载文件或程序集“Renci.SshNet,版本=2016.1.0.0,Culture=neutral,PublicKeyToken=1cee9f8bde3db106”或其依赖项之一。该系统找不到指定的文件

class CONNECT
{
    private MySqlConnection connection = new MySqlConnection("Datasource=localhost;Port=3306;Username=root;Password=;Database=Csharp_Hotel_DB");

    //create a function to return our connection
    public MySqlConnection getConnection()
    {
        return connection;
    }

    //create a function to open the connection
    public void openConnection()
    {
        if (connection.State == ConnectionState.Closed)
        {
            connection.Open();

        }
    }

    //create a function to close the connection
    public void closeConnection()
    {
        if (connection.State == ConnectionState.Open)
        {
            connection.Close();

        }
    }
}

private void buttonLogin_Click(object sender, EventArgs e)
{
    CONNECT conn = new CONNECT();
    DataTable table = new DataTable();
    MySqlDataAdapter adapter = new MySqlDataAdapter();
    MySqlCommand command = new MySqlCommand();
    String query = "SELECT * FROM `users` WHERE `username`=@usn AND `password`=@pass";

    command.CommandText = query;
    command.Connection = conn.getConnection();

    command.Parameters.Add("@usn", MySqlDbType.VarChar).Value = textBoxUsername.Text;
    command.Parameters.Add("@pass", MySqlDbType.VarChar).Value = textBoxPassword.Text;

    adapter.SelectCommand = command;
    adapter.Fill(table); //this line is the FileNotFoundException was unhandled


    // if the username and the password exists
    if (table.Rows.Count > 0)
    {
        this.Hide();
        MessageBox.Show("YES");
        Main_Form mform = new Main_Form();
        mform.Show();
    }
    else
    {

        if (textBoxUsername.Text.Trim().Equals(""))
        {
            MessageBox.Show("Enter Your Username to Login", "Empty Username", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else if (textBoxPassword.Text.Trim().Equals(""))
        {
            MessageBox.Show("Enter Your Password to Login", "Empty Password", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        { 
            MessageBox.Show("Username and Password Doesn't Exists", "Wrong Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
4

4 回答 4

1

它现在正在工作 c. 使用MySQL-connector-net-6.3.5可以修复此错误或问题,因为我使用的是 .net 4

感谢您的帮助。

于 2019-10-01T10:58:25.157 回答
0

似乎您没有处理异常 adapter.fill 可以通过。采用:

try{
    adapter.Fill(table);
} catch(FileNotFoundException e) {
    do stuff with e or your code
}

或者在使用它们之前检查路径是否存在。

于 2019-10-01T08:25:49.193 回答
0

您正在创建一个数据表,而没有将名称传递给构造函数。这意味着该表是无名的,因此当您尝试在无名表上调用 Fill 时,您会得到您的文件未找到异常。

你应该传入表名

DataTable table = new DataTable("users");

于 2019-10-01T08:33:43.537 回答
0

这是 MySQL Connector/NET 中的一个错误,错误 96614。它将在 Connector/NET 8.0.18 中修复。

在即将发布的 MySQL Connector/NET 8.0.18 版本中已修复,这是更改日志条目:

Renci.SshNet.dll 部署对于 Connector/NET 8.0.17 MSI 安装存在问题。因此,某些应用程序(例如 Microsoft Excel)无法读取 MySQL 数据。此修复删除了对 DLL 的不必要依赖,并确保 MSI 安装将正确的 Renci.SshNet.dll 部署到 GAC。

您今天可以使用的解决方法是切换到MySqlConnector,这是 MySQL 的替代 OSS ADO.NET 库,它没有此错误。

于 2019-10-01T23:55:59.830 回答