使用ADO.NET存取Image字段(急,在线等)

wpasp 2004-09-27 01:51:19
各位,在ADO.NET里如何存取Image等大型字段呢

要求:

1、不使用SQL存储过程;
2、使用流技术实现;
3、呵呵,快快帮我呀,急急。
...全文
161 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wpasp 2004-10-01
  • 打赏
  • 举报
回复
呵呵,楼上的各位大侠,我使用的是OleDB,不是SQL。我在使用OleDB时,一旦更新图片到数据库,就会出现错误,要我实现什么转换。我的InsertCommand是:

Insert into User(ID,Name,Age,Sex,Photo,Memo) Value(?,?,?,?,?,?)

然后各Parameters如下:
@ID, bigInt
@Name, varwchar
@Age, Integer
@Sex, varwchar
@Photo, LongVarBinary
@Memo, varwchar

我用的是Access数据库,其中Photo的字段类型设置的是"Ole 对象"


如果我不存Photo的话,即我将@Photo设为DBNull,可以通过.可是当我选择一个图片以后,一保存就出错,说必须实现什么"IConverit接口"???

各位再帮我!!!
njhyh 2004-09-28
  • 打赏
  • 举报
回复
'续!


'Function to Create Instance For the Image From the Buffer
Private Sub DrawToScale(ByVal bmp As Image)
PictureBox1.Image = New Bitmap(bmp)
End Sub

'显示处理
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button3.Click
Dim i As String = InputBox("请输入名字:")
ShowImage(i)
End Sub

'退出处理
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button4.Click
Me.Dispose()
End Sub

'删除处理
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button5.Click
cn = New SqlClient.SqlConnection(SqlConnection2.ConnectionString)
cn.Open()
Dim s As String = InputBox("请输入要删除的名字:")
Dim cmd As New SqlClient.SqlCommand("delete from photos where name='" & s & "'", cn)
cmd.ExecuteNonQuery()
MsgBox("Image deleted")
cn.Close()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class
'你新建一个工程,然后将代码考入看看~~~
njhyh 2004-09-28
  • 打赏
  • 举报
回复
'续

Me.SqlCommand1.CommandText = "dbo.[sp_InsertPhoto]"
Me.SqlCommand1.CommandType = System.Data.CommandType.StoredProcedure
Me.SqlCommand1.Connection = Me.SqlConnection1
Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50))
Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@image", System.Data.SqlDbType.VarBinary, 2147483647))
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(318, 298)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(95, 35)
Me.Button3.TabIndex = 6
Me.Button3.Text = "显示记录"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(434, 298)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(87, 35)
Me.Button4.TabIndex = 7
Me.Button4.Text = "退出"
'
'Button5
'
Me.Button5.Location = New System.Drawing.Point(200, 298)
Me.Button5.Name = "Button5"
Me.Button5.Size = New System.Drawing.Size(102, 35)
Me.Button5.TabIndex = 8
Me.Button5.Text = "删除该记录"
'
'SqlDataAdapter1
'
Me.SqlDataAdapter1.InsertCommand = Me.SqlInsertCommand1
Me.SqlDataAdapter1.SelectCommand = Me.SqlSelectCommand1
Me.SqlDataAdapter1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Photos", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("name", "name"), New System.Data.Common.DataColumnMapping("photo", "photo")})})
'
'SqlSelectCommand1
'
Me.SqlSelectCommand1.CommandText = "SELECT name, photo FROM Photos"
Me.SqlSelectCommand1.Connection = Me.SqlConnection2
'
'SqlInsertCommand1
'
Me.SqlInsertCommand1.CommandText = "INSERT INTO Photos(name, photo) VALUES (@name, @photo); SELECT name, photo FROM P" & _
"hotos"
Me.SqlInsertCommand1.Connection = Me.SqlConnection2
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"))
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@photo", System.Data.SqlDbType.VarBinary, 2147483647, "photo"))
'
'SqlConnection2
'
Me.SqlConnection2.ConnectionString = "workstation id=NJH;packet size=4096;integrated security=SSPI;data source=NJH;pers" & _
"ist security info=False;initial catalog=PerSonOperator"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(589, 348)
Me.Controls.Add(Me.PictureBox1)
Me.Controls.Add(Me.Button5)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Label2)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region
'Browse Button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
'Display Picture File
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

'Add Button
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button2.Click
' To Insert Image
Dim st As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim s As String = TextBox1.Text
Dim mbr As BinaryReader = New BinaryReader(st)
Dim buffer(st.Length) As Byte
mbr.Read(buffer, 0, CInt(st.Length))
st.Close()
InsertImage(buffer, s)
End Sub

'Function For Inserting in the Procdeure in the Database
Public Function InsertImage(ByRef buffer, ByVal str)
cn = New SqlClient.SqlConnection(SqlConnection2.ConnectionString)
cn.Open()
Dim cmd As New 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()
MsgBox("Image inserted")
cn.Close()
End Function

'Function to Display Image
Private Sub ShowImage(ByVal s As String)
cn = New SqlClient.SqlConnection(SqlConnection2.ConnectionString)
cn.Open()
Dim str As String = "SELECT photo FROM Photos WHERE name='" & s & "'"
Dim cmd As New SqlClient.SqlCommand(str, cn)
TextBox1.Text = s
Dim b() As Byte
b = cmd.ExecuteScalar()
If (b.Length > 0) Then
Dim stream As New MemoryStream(b, True)
stream.Write(b, 0, b.Length)
DrawToScale(New Bitmap(stream))
stream.Close()
End If
cn.Close()
End Sub
njhyh 2004-09-28
  • 打赏
  • 举报
回复
'sql数据库中需要有image类型的字段
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Dim cn As SqlClient.SqlConnection

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
Friend WithEvents SqlCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents Button5 As System.Windows.Forms.Button
Friend WithEvents SqlDataAdapter1 As System.Data.SqlClient.SqlDataAdapter
Friend WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlInsertCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlConnection2 As System.Data.SqlClient.SqlConnection
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
Me.SqlCommand1 = New System.Data.SqlClient.SqlCommand
Me.Button3 = New System.Windows.Forms.Button
Me.Button4 = New System.Windows.Forms.Button
Me.Button5 = New System.Windows.Forms.Button
Me.SqlDataAdapter1 = New System.Data.SqlClient.SqlDataAdapter
Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand
Me.SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand
Me.SqlConnection2 = New System.Data.SqlClient.SqlConnection
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(18, 20)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(96, 26)
Me.Label1.TabIndex = 0
Me.Label1.Text = "姓名:"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(13, 69)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(96, 21)
Me.Label2.TabIndex = 1
Me.Label2.Text = "图片"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(90, 17)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(208, 21)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = ""
'
'PictureBox1
'
Me.PictureBox1.Location = New System.Drawing.Point(82, 52)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(451, 239)
Me.PictureBox1.TabIndex = 3
Me.PictureBox1.TabStop = False
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(334, 14)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(115, 28)
Me.Button1.TabIndex = 4
Me.Button1.Text = "浏览图片…"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(68, 298)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(120, 35)
Me.Button2.TabIndex = 5
Me.Button2.Text = "添加到数据库"
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "Data Source=.;Initial Catalog=mxh;User Id=sa;Password=;"
'
'SqlCommand1
wpasp 2004-09-28
  • 打赏
  • 举报
回复
xiaohutushen(xiaohutushen) 朋友,你说的方法不行,我试了,在WinForm里老是提示需要实现IConverit..的接口.

请帮我,我要的是在WinForm实现,并且不可以使用SQL存储过程,要球纯代码实现图片存取功能!

Riny 2004-09-28
  • 打赏
  • 举报
回复
//存储图片到数据库

FileStream fs = new FileStream(openFile.FileName,FileMode.Open,FileAccess.Read);

byte[] buffer = new buffer[fs.Length];

fs.Read(buffer,0,buffer.Length);

//sql语句

string sqlStr = String.Format("insert into sysuser (userPhoto) values ('{0}'",@userPhoto);

comm.Parameters.Add("@userPhoto",SqlDbType.Image);

comm.Parameters["@userPhoto"].Value=buffer

//从数据库里读图片

while(dr.Read())

{

byte[] buffer = (byte[])dr["userPhoto"];

MemoryStream ms = new MemoryStream(buffer,0,buffer.Length);

Image img;

img = img.FromStream(ms);

pictureBox1.Image= img;

sssmmm 2004-09-27
  • 打赏
  • 举报
回复
关注
xiaohutushen 2004-09-27
  • 打赏
  • 举报
回复
http://www.knowsky.com/3257.html

16,553

社区成员

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

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