如何避免使用带有vb代码的asp.net将重复数据插入到sql数据库中 [英] How to avoid inserting duplicated data into a sql database using asp.net with vb code
本文介绍了如何避免使用带有vb代码的asp.net将重复数据插入到sql数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Try
If IsPostBack() Then
Dim objConn As New SqlConnection(WebConfigurationManager.ConnectionStrings("cnxCurso").ConnectionString)
Dim sSQL, Resul As String
sSQL = "Insert into dbo.actor (dpi, nombre, comunidad, fecha, genero, usuario) values ('" & txtdpi.Text & "', '" & txtactor.Text & "', '" & txtcomunidad.Text & "', '" & txtfecha.Text & "', '" & genero.Text & "', '" & txtusuario.Text & "')"
Dim objCmd As New SqlCommand(sSQL, objConn)
objConn.Open()
Resul = objCmd.ExecuteNonQuery
If Resul Then
'MsgBox("Se ha agregado correctamente el registro")
respuesta.Visible = True
respuesta.Text = "Registro agregado"
Response.Redirect("mantenimiento.aspx", True)
objConn.Close()
Else
'MessageBox.Show("No se agregado correctamente el registro")
respuesta.Visible = True
respuesta.Text = "Registro no agregado"
End If
End If
Catch ex As SqlException
End Try
respuesta.Text = "Registro agregado"
End Sub
推荐答案
简单方法是读取数据库2.比较结果3如果存在做某事
例如我们有一个名为ice_cream.mdb的数据库,它有一个记录chocolatte ...
因此,当你启动应用程序时,它应该读取记录并将所有记录保存在某处,例如文件或其他内容......所以当你将添加新记录时,它应该读取文件,如果记录存在...当一个名字记录存在显示消息框并终止插入!如果不存在则执行查询...对不起我的低英语..希望我帮助你...
The Easy way is to read the database 2. compare results 3 if exist do something
for example we have database called ice_cream.mdb that it have one record chocolatte...
so when u start the application it should read the records and save all the record somewhere for example in a file or something else... so when u will add new record it should read the file if record exist... when a name of record exist show a messagebox and terminate insertion! if not exist execute the query... sorry for my low english.. hope i helped you...
这篇关于如何避免使用带有vb代码的asp.net将重复数据插入到sql数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文