【发布时间】:2015-08-13 06:17:42
【问题描述】:
当用户选择图像时,我想将图像保存在 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) { }
}
【问题讨论】:
标签: c# sql-server image bytearray sql-insert