有两个选择,1是上面说的,修改sqlcommand的调用方式,这样效率最高, 2,可以使用自己的编码方式,吧byte[]转换成字符串,然后保存到数据库的Text类型字段里,读取时反过来就可以,例如: byte[] b = new byte[xxx]; //read data to byte[] string s = StringEncode(b);//这里举个例,直接转换成hex编码或转换成base64也可以, //save to database //read data string s = ......//读出byte[]数据。 byte[] b = StringDecode(s);//反操作,吧hex编码或base64编码再转换成byte[]
command.Parameters.AddWithValue("@image_data",byteImage );
SqlConnection conn = new SqlConnection(@"data source=.;uid=sa;pwd=;database=master");
conn.Open();
SqlCommand cmd = new SqlCommand("insert into image values(@i)", conn);
byte[] ib = new byte[60000];
FileStream fs = new FileStream(this.openFileDialog1.FileName.ToString(), FileMode.Open, FileAccess.Read);
fs.Read(ib, 0, 60000);
cmd.Parameters.Add("@i", SqlDbType.Image, (int)fs.Length);
cmd.Parameters["@i"].Value = ib;
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("保存成功");
//显示图片:
SqlConnection conn = new SqlConnection(@"data source=.;uid=sa;pwd=;database=master");
conn.Open();
SqlCommand cmd = new SqlCommand("select image1 from image", conn);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
MemoryStream buf = new MemoryStream((byte[])reader[i]);
Image image = Image.FromStream(buf,true);
this.pictureBox1.Image = image;
}
}
http://dotnet.aspx.cc/article/9154bc99-df64-4e2d-b096-26c99ce464be/read.aspx
因为 连库的代码 都已经封装好了
只能传string
2,可以使用自己的编码方式,吧byte[]转换成字符串,然后保存到数据库的Text类型字段里,读取时反过来就可以,例如:
byte[] b = new byte[xxx];
//read data to byte[]
string s = StringEncode(b);//这里举个例,直接转换成hex编码或转换成base64也可以,
//save to database
//read data
string s = ......//读出byte[]数据。
byte[] b = StringDecode(s);//反操作,吧hex编码或base64编码再转换成byte[]
stream.Read(buffer, 0, (int)stream.Length);
stream.Close(); SqlCommand cmd = new SqlCommand("", sqlConn);
cmd.Parameters.Add("@photo", SqlDbType.Image).Value = buffer;
http://topic.csdn.net/u/20100419/20/5f087418-0d24-4bc5-a652-a7a4d0fc95d9.html
- -