使用vb更新sqlserver中的映像 [英] update images in sqlserver using vb

查看:101
本文介绍了使用vb更新sqlserver中的映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库表是tbltest,具有id(int),name(varchar),lastname(varchar),foto(数据类型图片)
我可以在数据库中插入数据,但现在不更新它们
我已经将数据存储在数据库中,并且我将使用按钮(更新)来更新它们
我的插入按钮的代码是

my db table is tbltest with id(int),name(varchar),lastname(varchar),foto(datatype image)
i can insert data in my database but i don''t now to update them
i have store data in my db ,and i will to update them with a button (update)
the code for my insert button is

cmdtabela1 = connetion.CreateCommand
                  cmdtabela1.CommandText = "insert into tbltest values (@name,@lastname,@foto) "

                  cmdtabela1.Parameters.AddWithValue("name", txtname.Text)
                  cmdtabela1.Parameters.AddWithValue("lastname", txtlastname.Text)
                  


                  Dim ms As New MemoryStream()
                  PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)
                  Dim data As Byte() = ms.GetBuffer()
                  Dim p As New SqlParameter("foto", SqlDbType.Image)
                  p.Value = data
                  cmdtabela1.Parameters.Add(p)



现在如何在我的数据库中更新此列

我已经为我的更新按钮编写了这段代码



now how to update this colums in my database

i have write this code for my update button

connetion = New SqlConnection("my conetion string")
                    connetion.Open()

                    Dim ms As New MemoryStream()
                    PictureBox3.BackgroundImage.Save(ms, PictureBox3.BackgroundImage.RawFormat)
                    Dim data As Byte() = ms.GetBuffer()
                    Dim p As New SqlParameter("Foto", SqlDbType.Image)
                    p.Value = data
                    cmdtabela.Parameters.Add(p)

                    cmdtabela = connetion.CreateCommand
                    cmdtabela.CommandText = "update tbltest set name='" & txtupdatename.Text & "', lastname='" & txtupdatelstname.Text &   where Id='" & Trim(txtid.Text) & "'"

推荐答案

使用参数化语句,例如在您的插入内容中:

Use a parameterized statement like in your insert:

connetion = New SqlConnection("my conetion string")
connetion.Open()

cmdtabela = connetion.CreateCommand
cmdtabela.CommandText = "update tbltest set name=@name, lastname=@lastname, foto=@foto where Id=@id"

cmdtabela.Parameters.AddWithValue("name", txtname.Text)
cmdtabela.Parameters.AddWithValue("lastname", txtlastname.Text)
cmdtabela.Parameters.AddWithValue("id", txtid.Text)

Dim ms As New MemoryStream()
PictureBox3.BackgroundImage.Save(ms, PictureBox3.BackgroundImage.RawFormat)
Dim data As Byte() = ms.GetBuffer()
Dim p As New SqlParameter("Foto", SqlDbType.Image)
p.Value = data
cmdtabela.Parameters.Add(p)



为安全起见,始终值得对您对数据库运行的任何查询或语句进行参数设置:

https://www.owasp.org/index.php/SQL_Injection [



It''s always worth parameterising any query or statement you run against a database for security:

https://www.owasp.org/index.php/SQL_Injection[^]


这篇关于使用vb更新sqlserver中的映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆