int O_id =Convert.ToInt32(textBox2.Text);
SqlConnection cn = new SqlConnection(strCn);
SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData, O_id) VALUES (@BLOBData,'"+O_id+"')", cn);
String strBLOBFilePath = textBox1.Text;//Modify this path as needed.
//Read jpg into file stream, and from there into Byte array.
FileStream fsBLOBFile = new FileStream(strBLOBFilePath, FileMode.Open, FileAccess.Read);
Byte[] bytBLOBData = new Byte[fsBLOBFile.Length];
fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length);
fsBLOBFile.Close();
//Create parameter for insert command and add to SqlCommand object.
SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,
0, 0, null, DataRowVersion.Current, bytBLOBData);
cmd.Parameters.Add(prm);
//Open connection, execute query, and close connection.
cn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Picture has been uploaded");
cn.Close();
图像几乎总是二进制形式。如果您要将图像存储在数据库中,则几乎可以确定将其存储为二进制文件。你的问题不是很清楚。你可以更具体一些,也许给一些代码示例? – 2011-03-01 06:18:43
图像如何存储在数据库中(代码示例更可取)? – 2011-03-01 06:18:57
“我没有把图像变成二进制形式” - 所以; **它是如何存储在数据库中的?什么字段类型?以及您使用的是什么数据访问工具? – 2011-03-01 06:19:11