如何在vb.net中的表中插入数据但避免重复 [英] how to insert data in tables in vb.net but avoid duplication

查看:115
本文介绍了如何在vb.net中的表中插入数据但避免重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用vb.net在表中插入数据,但在插入检查重复项之前

how to insert data in tables using vb.net,but before inserting checks duplication

推荐答案

请先让我发表您的努力,然后再提出连接方面的帮助是DML的基础,而DML在大学或课堂中都被认为是.NET ...的一部分.
现在开始查询

在表中使用主键约束作为要保持唯一性的值
现在进入连接部分的代码是
Please i would ask you to post in your efforts first and then ask for assistance as connectivity is the basic along with DML which is thought in colleges or class as part of .NET....

Now coming to your query

use a primary key constraint with your table for the value which you want to keep unique
now coming to the connectivity part hers the code
Imports System.Data.SqlClient ''use this namespace

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '' it will first check whether the textbox contains data or not
        If TextBox1.TextLength = 0 Then
            ''if textbox does not contain data then prompt the user to enter data
            ''with the help of messagebox
            MessageBox.Show("value missing")
        Else
            ''creating the object of sqlconnection class and setting its
            ''connectionstring property
            Dim con As SqlConnection = New SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True")
            ''open the connection
            con.Open()
            ''set the command by creating the object of sqlcommand class
            Dim cmd As SqlCommand = New SqlCommand("insert into mytable values(''" + TextBox1.Text + "'')", con)
            ''execute the query
            Dim x As Integer = cmd.ExecuteNonQuery()
            If (x <> 0) Then
                ''once the data is added display this messagebox to user
                MessageBox.Show("data added")
            End If
            ''close the connection in the end
            con.Close()
        End If
    End Sub
End Class


一旦找到有用的答案,就对我的答案进行评分
谢谢&问候
基数:rose:


Do rate my answer once you find it useful
Thanks & Regards
Radix :rose:


我正在使用访问数据库.在cmd.eecutenonquery()上显示错误.
I M USING ACCESS DATABASE. showing error on cmd.eecutenonquery().


这篇关于如何在vb.net中的表中插入数据但避免重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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