vb.net中实现将本地图片插入到SQLServer数据中(在线等)

jiuaidaxin19 2009-03-16 04:43:00
各位大侠,我想将本地的图片插入到数据库中,再从数据库中读取出来,帮帮忙吧!
...全文
884 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenjunliang2008 2010-05-06
  • 打赏
  • 举报
回复
可惜不是我想要的。学习了
dyc0020541 2010-04-19
  • 打赏
  • 举报
回复
看不到啊
xjcharm 2010-03-09
  • 打赏
  • 举报
回复
kankan jielun
wzuomin 2009-03-16
  • 打赏
  • 举报
回复

Public Shared Function GetImgBySQL(ByVal FileldName As String, ByVal TableName As String, ByVal sql As String) As Image
Dim cmdText As String = Conversions.ToString(Operators.ConcatenateObject((("select top 1 " & FileldName) & " from " & TableName), Interaction.IIf((sql <> ""), (" where " & sql), "")))
Dim connection As New SqlConnection(MySql.GetConnToSqlServer(True))
Dim image2 As Image = Nothing
Try
Dim command As New SqlCommand(cmdText, connection)
command.CommandType = CommandType.Text
connection.Open
Dim buffer As Byte() = DirectCast(command.ExecuteScalar, Byte())
command.Dispose
If (buffer Is Nothing) Then
Return image2
End If
If (buffer.Length > 0) Then
Dim stream As New MemoryStream(buffer)
image2 = Image.FromStream(stream)
stream.Close
stream = Nothing
End If
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exception As Exception = exception1
Throw New NotSupportedException(exception.Message)
ProjectData.ClearProjectError
Finally
connection.Close
End Try
Return image2
End Function

Public Shared Function UpdateImgBySQL(ByVal TableName As String, ByVal FieldName As String, ByVal ImgFieldValue As String, ByVal ParaSQL As String) As Integer
If (ImgFieldValue = "") Then
Return 0
End If
Dim cmdText As String = Conversions.ToString(Operators.ConcatenateObject((("update " & TableName & " set ") & FieldName & " = @img "), Interaction.IIf((ParaSQL <> ""), (" where " & ParaSQL), "")))
Dim num As Integer = 0
Dim connection As New SqlConnection(MySql.GetConnToSqlServer(True))
Try
Dim command As New SqlCommand(cmdText, connection)
connection.Open
Dim stream As New FileStream(ImgFieldValue, FileMode.Open, FileAccess.Read)
Dim array As Byte() = New Byte((CInt(stream.Length) + 1) - 1) {}
stream.Read(array, 0, CInt(stream.Length))
stream.Close
stream = Nothing
command.Parameters.Add("@img", SqlDbType.Image).Value = array
num = command.ExecuteNonQuery
command.Dispose
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exception As Exception = exception1
Throw New Exception(exception.Message)
ProjectData.ClearProjectError
Finally
connection.Close
End Try
Return num
End Function




http://topic.csdn.net/u/20090206/07/841895bb-3d1c-4b2a-9e28-c76138b6ba2d.html
wuyq11 2009-03-16
  • 打赏
  • 举报
回复
Private Sub InsertImage()Sub InsertImage() '把图片插入到数据库中
Dim MyStream As New FileStream("", FileMode.Open, 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=.;Initial Catalog=a;User ID=sa;Password=SA"
Dim MyConnection As New SqlConnection(SqlConnectionString)
Dim MyCommand As New SqlCommand("INSERT INTO Photos ([name], [photo]) VALUES (@name, @image)", MyConnection)
MyCommand.Parameters.Add("@name", "test")
MyCommand.Parameters.Add("@image", buffer)
MyConnection.Open()
MyCommand.ExecuteNonQuery()
MyConnection.Close()
End Sub


Private Sub ShowImage()Sub ShowImage()
Dim SqlConnectionString As String = "Persist Security Info=False;Data Source=.;Initial Catalog=A;User ID=sa;Password=sa"
Dim MyConnection As New SqlConnection(SqlConnectionString)
MyConnection.Open()
Dim MyCommand As New SqlClient.SqlCommand("SELECT photo FROM Photos WHERE [name]='test'", MyConnection)
Dim buffer() As Byte = MyCommand.ExecuteScalar()
If (Not buffer Is Nothing) AndAlso (buffer.Length > 0) Then
Dim stream As New MemoryStream(buffer, 0, buffer.Length, True)
PictureBox1.Image = New Bitmap(stream)
stream.Close()
End If
MyConnection.Close()
End Sub

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧