PrivateSub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'打开对话框选择要保存的图片 OpenFileDialog1.InitialDirectory ="d:/pic" OpenFileDialog1.DefaultExt ="gif" OpenFileDialog1.Filter ="Bmp Files(*.bmp)|*.bmp|Gif Files(*.gif)|*.gif|Jpg Files(*.jpg)|*.jpg" OpenFileDialog1.ShowDialog() PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName) End Sub
PrivateSub Button2_Click()Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ' 图片当作流读入 Dim st AsNew FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read) Dim s AsString= TextBox1.Text Dim mbr As BinaryReader =New BinaryReader(st) Dim buffer(st.Length) AsByte mbr.Read(buffer, 0, CInt(st.Length)) st.Close() InsertImage(buffer, s)
End Sub '后台数据库操作 PublicFunction InsertImage()Function InsertImage(ByRef buffer, ByValstr) cn =New SqlClient.SqlConnection(SqlConnection1.ConnectionString) cn.Open() Dim cmd AsNew SqlClient.SqlCommand("sp_InsertPhoto", cn) cmd.CommandType = CommandType.StoredProcedure cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer cmd.ExecuteNonQuery()