system.invalidoperationException:'连接属性不得为null'在MySQL连接中

发布于 2025-02-11 13:12:17 字数 1126 浏览 1 评论 0原文

在测试项目时,我遇到了错误 “ system.invalidoperationException:'连接属性必须是非零件 。 尝试解决错误,我编辑了数据库值并重新定义Enter_email和电子邮件条目。在网上寻找问题的原因,我找到了此错误的页面,但是,没有提到我的消息或发现它的原因。我将服务器信息放入一个单独的变量中。我不确定造成此错误的原因。有人可以向我解释吗?

给出错误的代码在这里:

async void LoginClick(object sender, EventArgs args)
    {
        var Server = "server=myip;user=app;database=travel_logger;port=3306;password=app; default command timeout=20;";
        string enter_email = Email.Text;
        using (var connection = new MySqlConnection(Server))
        {
            connection.Open();
            var cmd = new MySqlCommand("SELECT _Password FROM * WHERE email=" + enter_email);
            var reader = await cmd.ExecuteReaderAsync();
            string data_password = reader.GetString(0);
            if (data_password == Password.Text)
            {
                await Navigation.PushAsync(new AboutPage());
            }
            else
            {
                await DisplayAlert("Login Error", "Incorrect email or password","Okay");
            }
            connection.Close();
        }; 
    }

谢谢!

While Testing the project I came across the error "System.InvalidOperationException:'Connection property must be non-null.
Attempting to fix the error, I edited database values and re-defined enter_email and the Email Entry. Looking online to find the cause of the problem, I found the page for this error, however, no reference to my message or the cause of it was found. I placed the server info into a separate variable. I'm unsure of the cause of this error. Could anyone explain it to me?

The code giving the error is here:

async void LoginClick(object sender, EventArgs args)
    {
        var Server = "server=myip;user=app;database=travel_logger;port=3306;password=app; default command timeout=20;";
        string enter_email = Email.Text;
        using (var connection = new MySqlConnection(Server))
        {
            connection.Open();
            var cmd = new MySqlCommand("SELECT _Password FROM * WHERE email=" + enter_email);
            var reader = await cmd.ExecuteReaderAsync();
            string data_password = reader.GetString(0);
            if (data_password == Password.Text)
            {
                await Navigation.PushAsync(new AboutPage());
            }
            else
            {
                await DisplayAlert("Login Error", "Incorrect email or password","Okay");
            }
            connection.Close();
        }; 
    }

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪化雨蝶 2025-02-18 13:12:17

这是投掷错误,因为您的命令不知道您的命令使用哪种连接。初始化时,您需要将连接传递到mySqlCommand

var cmd = new MySqlCommand("SELECT _Password FROM * WHERE email=" + enter_email);
cmd.Connection = connection; // <- add this row

It's throw error because your command doesn't know which connection your command use. You need to pass your connection to your MySqlCommand when initialize.

var cmd = new MySqlCommand("SELECT _Password FROM * WHERE email=" + enter_email);
cmd.Connection = connection; // <- add this row
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文