system.invalidoperationException:'连接必须有效并打开。'

发布于 2025-01-28 23:03:06 字数 856 浏览 5 评论 0原文

我正在使用.NET和MySQL数据库在C#中开发一个程序。 一旦登录用户,我就需要可见用户图像,为此,我有一个称为“ image”的Longblob类型列。登录过程很好,但是发生以下错误,

Connection must be valid and open.

我正在使用以下代码从数据库中进行选择:

MySqlCommand command1 = new MySqlCommand("select `user Image` from `userdb` where `username` = @img", con);
        
        command1.Parameters.Add("@img", MySqlDbType.Blob);
        
        command1.Parameters["@img"].Value = Admin;

        byte[] img = (byte[])command1.ExecuteScalar();

        if (img == null)
        {
            pic_profilePic.Image = null;
        }
        else
        {
            var DATA = (byte[])command1.ExecuteScalar();
            var stream = new MemoryStream(DATA);
            pic_profilePic.Image = Image.FromStream(stream);
        }

尽管将列类型更改为二进制和Varbinary,但我仍然遇到相同的错误。

有人知道我在这里做错了吗?

I am developing a program in C # with .NET and MySQL databases.
I need the user image to be visible once the user is logged in, for which I have a LONGBLOB type column called 'Image'. The login process was good, but the following error occurred

Connection must be valid and open.

I'm using the following code to select from the database:

MySqlCommand command1 = new MySqlCommand("select `user Image` from `userdb` where `username` = @img", con);
        
        command1.Parameters.Add("@img", MySqlDbType.Blob);
        
        command1.Parameters["@img"].Value = Admin;

        byte[] img = (byte[])command1.ExecuteScalar();

        if (img == null)
        {
            pic_profilePic.Image = null;
        }
        else
        {
            var DATA = (byte[])command1.ExecuteScalar();
            var stream = new MemoryStream(DATA);
            pic_profilePic.Image = Image.FromStream(stream);
        }

Despite changing the column type into binary and varbinary, I still got the same error.

Does anyone know what I'm doing wrong here?

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

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

发布评论

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

评论(1

听不够的曲调 2025-02-04 23:03:06
string cs = @"server=localhost;userid=dbuser;password=s$cret;database=testdb";

var con = new MySqlConnection(cs);
con.Open(); //did you open after you initialize the connection?

请检查您的代码以打开您的

连接

con.Close();
string cs = @"server=localhost;userid=dbuser;password=s$cret;database=testdb";

var con = new MySqlConnection(cs);
con.Open(); //did you open after you initialize the connection?

Please check your code to open your connection

Also remember to close it after you are done

con.Close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文