gpt4 book ai didi

c# - 系统。无效操作异常

转载 作者:行者123 更新时间:2023-11-28 23:17:22 30 4
gpt4 key购买 nike

我正在尝试制作一个带有登录系统的程序我对此很陌生,但我已经连续工作 8 个小时试图解决这个问题。这是我得到的错误代码

+ ServerVersion 'con.ServerVersion' threw an exception of type 'System.InvalidOperationException'   string {System.InvalidOperationException}

这是我的代码

    private void LogB_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=Myip;user id=MyId;database=MyDb;password=MyPw;persistsecurityinfo=True");
SqlDataAdapter sda = new SqlDataAdapter("Select * From login where navn='"+ TULog.Text + "' and pw='" + TPLog.Text + "'",con);

try
{

con.Open();


}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
throw ex;
}
finally
{
con.Close();
}
}
}
}

抱歉,如果这看起来有些废话,但我是一个正在努力学习的人 :p

最佳答案

此异常告诉您在 SqlConnection 关闭时尝试访问 con.ServerVersion 属性。

来自 SqlConnection.ServerVersion 上的 MSDN属性:

InvalidOperationException - The connection is closed. ServerVersion was called while the returned Task was not completed and the connection was not opened after a call to OpenAsync.

您在上面显示的代码没有显示对此属性的调用,您必须在其他地方这样做。无论如何,在这样做之前需要打开连接。

关于c# - 系统。无效操作异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43298264/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com