Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用vb.net检查记录是否存在,如果不存在,如何插入?_Mysql_Vb.net - Fatal编程技术网

Mysql 如何使用vb.net检查记录是否存在,如果不存在,如何插入?

Mysql 如何使用vb.net检查记录是否存在,如果不存在,如何插入?,mysql,vb.net,Mysql,Vb.net,我一直在检查数据库中是否存在记录。这在php中很容易,但我找不到一个好的教程如何在vb.net中实现它。如果数据库中不存在文本框,我想从文本框中插入值 这是我的密码: Using SQLConnection As New MySqlConnection(connString) Using sqlCommand As New MySqlCommand() With sqlCommand 'check if record exist

我一直在检查数据库中是否存在记录。这在php中很容易,但我找不到一个好的教程如何在vb.net中实现它。如果数据库中不存在文本框,我想从文本框中插入值

这是我的密码:

Using SQLConnection As New MySqlConnection(connString)


        Using sqlCommand As New MySqlCommand()
            With sqlCommand
            'check if record exist 

            'if not execute these
                .CommandText = "INSERT INTO bookrecords (Title, Author, Edition, Publisher, ISBN) values (@title,@author,@edition,@publisher,@isbn)"
                .Connection = SQLConnection
                .CommandType = CommandType.Text
                .Parameters.AddWithValue("@title", txtTitle.Text)
                .Parameters.AddWithValue("@author", txtAuthor.Text)
                .Parameters.AddWithValue("@edition", txtEdition.Text)
                .Parameters.AddWithValue("@publisher", txtPublisher.Text)
                .Parameters.AddWithValue("@isbn", txtISBN.Text)

            End With


            Try
                SQLConnection.ConnectionString = "Server=localhost;Database=booksdb;Uid=root;Pwd=;"
                SQLConnection.Open()
                sqlCommand.ExecuteNonQuery()
                iReturn = True
                'MessageBox.Show("Connection Opened")

            Catch ex As MySqlException
                MessageBox.Show("Error: " & ex.ToString())
                iReturn = False
            Finally
                SQLConnection.Close()
                'MessageBox.Show("Connection Closed")
            End Try

        End Using
    End Using
我只希望@isbn是确定记录是否已经存在的关键。

显然要做的事情(不涉及数据库架构)是向数据库发送所需isbn的计数查询。
这可以通过ExecuteScalar和适当的查询来完成

Using con = new MySqlConnection(connectionString)
    Using sqlCommand As New MySqlCommand()
        With sqlCommand
            .Connection = con
            .Parameters.AddWithValue("@isbn", txtISBN.Text)
            .CommandText = "SELECT COUNT(*) FROM  bookrecords WHERE ISBN = @isbn"
        End With
        con.Open()
        Dim result = sqlCommand.ExecuteScalar()
        if Convert.ToInt32(result) = 0 Then
            ' ISBN doesn't exist ....
            sqlCommand.Parameters.Clear()
            ' rest of your insert code ...

如果该命令返回的计数为零,请记住清除后续插入查询的参数集合

,您可以在一个查询中执行类似以下操作:

.CommandText = "INSERT INTO bookrecords (Title, Author, Edition, Publisher, ISBN) " & _
"SELECT * FROM (SELECT @title, @author, @edition, @publisher, @isbn AS ISBN) t " & _
"WHERE NOT EXISTS(SELECT ISBN FROM bookrecords b WHERE t.ISBN = b.ISBN ) "

isbn
上添加一个
UNIQUE
keu,使用
INSERT IGNORE
。或者您可以完全在SQL中执行相同的操作(至少可以使用MSSQL—我假设MySQL是相同的)