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