连接必须是有效且打开的(executeScalar())

本文关键字:executeScalar 有效 连接 | 更新日期: 2023-09-27 18:18:28

给我这个错误connection must be valid and open,我不知道如何解决它。

    private void loginUtenti(string user, string psw)
    {
        createConnection(); 
        string connectionString = "server=localhost;user=root;database=test;port=3306;password=******;"; ;
        MySqlConnection connection = new MySqlConnection(connectionString);
        command = new MySqlCommand();
        command.CommandText = "SELECT Psw FROM Utenti WHERE Nome = '" + user + "';";
        object passwordInput = command.ExecuteScalar(); // error "connection must be valid and open" 
        string nullo = "";

连接必须是有效且打开的(executeScalar())

尝试将命令与连接关联

    command.Connection = connection;

另外,'createConnection' 做什么?

并使用准备好的语句/绑定变量。这是SQL注入等待发生

将预期的连接分配给Command对象,并在使用cmd.ExecuteScalar()

执行查询之前打开相同的连接