gpt4 book ai didi

c# - 在sql数据库中保存图像C#

转载 作者:行者123 更新时间:2023-11-30 21:57:40 24 4
gpt4 key购买 nike

我想在用户选择时将图像保存在 SQL 数据库中。到目前为止,我已经输入了这段代码。这不会给我任何错误,但不会添加到数据库中。我认为 SQL 语句有问题。

谁能帮帮我?

这是我的代码:

public void addImages(string tag1,string tag2,string tag3,string status,string fileName)
{
try
{
byte[] image = null;
FileStream fsstream = new FileStream(fileName,FileMode.Open,FileAccess.Read);
BinaryReader br = new BinaryReader(fsstream);
image = br.ReadBytes((int)fsstream.Length);

SqlCommand command = new SqlCommand("INSERT INTO [ImagesAndTags] (Images,Tags,Tag2,Tag3,Status) values (@IMG,'" + tag1 + "','" + tag2 + "','" + tag3 + "','" + status + "')", con);
con.Open();
command.Parameters.Add(new SqlParameter("@IMG",image));
SqlDataReader reader = command.ExecuteReader();
MessageBox.Show("Added Successfully!!!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
while (reader.Read()) { }
}
catch(Exception ex) { }
}

最佳答案

ExecuteReader 返回数据。在你的情况下,你不是。您只需尝试在数据库中插入一行。这就是为什么你需要使用 ExecuteNonQuery相反。

并像对 image 变量所做的那样参数化其他插入值。也可以使用 using statement处理您的数据库连接和命令。

int insertedRowCount = command.ExecuteNonQuery();

if(insertedRowCount > 0)
MessageBox.Show("Added Successfully!!!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

关于c# - 在sql数据库中保存图像C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30545540/

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