Private Sub InsertImage() '把图片插入到数据库中
Dim MyStream As New IO.FileStream(TextBox1.Text, IO.FileMode.Open, IO.FileAccess.Read)
Dim buffer(MyStream.Length) As Byte
MyStream.Read(buffer, 0, MyStream.Length)
MyStream.Close()
Dim SqlConnectionString As String = "Persist Security Info=False;Data Source=(local);Initial Catalog=DZBL;User ID=sa;Password=123"
Dim MyConnection As New SqlClient.SqlConnection(SqlConnectionString)
Dim MyCommand As New SqlClient.SqlCommand("INSERT INTO gonggongmoban ([mobanbianhao],[mobanming],[mobanbeizhuxinxi],[moban]) VALUES (@mobanbianhao, @mobanming, @mobanbeizhuxinxi, @moban)", MyConnection)
MyCommand.Parameters.AddWithValue("@mobanbianhao", TextBox3.Text)
MyCommand.Parameters.AddWithValue("@mobanming", TextBox2.Text)
MyCommand.Parameters.AddWithValue("@mobanbeizhuxinxi", TextBox4.Text)
MyCommand.Parameters.AddWithValue("@moban", buffer)
MyConnection.Open()
MyCommand.ExecuteNonQuery()
MyConnection.Close()
End Sub
以上是上传图片或资源到数据库的代码,这里是打开对话框选择文件上传的
有没有办法改进下,默认路径和文件名上传,不需要再弹出如上的对话框选择。
------解决思路----------------------
直接将你要存入数据库的图片转为二进制存入数据库