adapter.Fill(table) 行未找到文件异常未处理

File Not Found Exception was unhandled in the line of adapter.Fill(table)

我正在尝试连接到数据库 MySql 但出现了此错误

FileNotFoundException was unhandled

adapter.Fill 行中。

FileNotFoundException 未处理 无法加载文件或程序集 'Renci.SshNet, Version=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);
        }
    }
}

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

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

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

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

您应该传入 table 名称

DataTable table = new DataTable("users");

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

感谢您的帮助。

这是 MySQL Connector/NET、bug 96614 中的错误。它将在 Connector/NET 8.0.18.

中修复

Fixed as of the upcoming MySQL Connector/NET 8.0.18 release, and here's the changelog entry:

The Renci.SshNet.dll deployment was problematic for Connector/NET 8.0.17 MSI installations. Some applications, such as Microsoft Excel, were unable to read MySQL data as a result. This fix removes unnecessary dependencies on the DLL and also ensures that the MSI installation deploys the correct Renci.SshNet.dll to the GAC.

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