Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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
Sql vb2008使用一个按钮插入和更新记录_Sql_Vb.net - Fatal编程技术网

Sql vb2008使用一个按钮插入和更新记录

Sql vb2008使用一个按钮插入和更新记录,sql,vb.net,Sql,Vb.net,我正在试着编一个词汇表。我有一个带有列表框、两个文本框和一个保存按钮的表单 列表框现在由数据库中的单词填充,当选择一个单词时,其定义将显示在textbox2中 用户可以通过在textbox1中填入一个新词,在textbox2中填入其定义,然后单击save按钮来添加记录。如果新词已经存在,则不允许保存新记录,如果两个文本框之间存在空值。如果不存在,则会将其插入表中,并将新词添加到列表框中 用户还可以通过先选择列表中的一个单词,然后编辑单词和/或定义并单击保存按钮来更新记录 我已经得到了更新部分的工

我正在试着编一个词汇表。我有一个带有列表框、两个文本框和一个保存按钮的表单

列表框现在由数据库中的单词填充,当选择一个单词时,其定义将显示在textbox2中

用户可以通过在textbox1中填入一个新词,在textbox2中填入其定义,然后单击save按钮来添加记录。如果新词已经存在,则不允许保存新记录,如果两个文本框之间存在空值。如果不存在,则会将其插入表中,并将新词添加到列表框中

用户还可以通过先选择列表中的一个单词,然后编辑单词和/或定义并单击保存按钮来更新记录

我已经得到了更新部分的工作,但我有问题,在插入一个新的记录。我做不好。词汇表只有两个字段:单词、定义。这是我的密码:

Dim myCmd As New MySqlCommand
Dim myReader As MySqlDataReader
Dim myAdptr As New MySqlDataAdapter
Dim myDataTable As New DataTable
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Call Connect()
    With Me
        If Blank() = False Then
            If Duplicate() = False Then
                STRSQL = "insert into glossary values (@word, @def)"
                myCmd.Connection = myConn
                myCmd.CommandText = STRSQL
                myCmd.Parameters.AddWithValue("word", txtNew.Text)
                myCmd.Parameters.AddWithValue("def", txtdefine.Text)
                myCmd.ExecuteNonQuery()
                myCmd.Dispose()
                MsgBox("Record Added")
                Dim word As String
                word = txtNew.Text
                lstword.Items.Add(word)
                'myConn.Close()
                'Me.FillListbox()
            Else
                myConn.Open()
                STRSQL = "Update glossary set word = @term, definition = @mean where word = @term"
                myCmd.Connection = myConn
                myCmd.CommandText = STRSQL
                myCmd.Parameters.AddWithValue("term", txtNew.Text)
                myCmd.Parameters.AddWithValue("mean", txtdefine.Text)
                myCmd.ExecuteNonQuery()
                myCmd.Dispose()
                MsgBox("Record Updated", MsgBoxStyle.Information, "New word added")
            End If
        End If
    End With


End Sub

Public Function Blank() As Boolean
    Call Connect()
    With Me
        If .txtNew.Text = "" Or .txtdefine.Text = "" Then
            Blank = True
            MsgBox("Cannot save! Term and definition should not contain null value", MsgBoxStyle.Critical, "Unable to save")
        Else
            Blank = False
        End If
    End With
End Function

Public Function Duplicate() As Boolean
    Call Connect()
    With Me
        STRSQL = "Select * from glossary where word = '" & txtNew.Text & "'"
        myCmd.Connection = myConn
        myCmd.CommandText = STRSQL
        If myDataTable.Rows.Count <> 0 Then
            Duplicate = True
            'MsgBox("Word already exist. Please check the word.", MsgBoxStyle.Critical, "Duplicate.")

        Else
            Duplicate = False
        End If
        myConn.Close()
    End With
End Function

如何使其正常工作?

您在上述所有代码中都使用了全局变量

myConn and myCmd 
特别是,您在myCmd上调用Dispose,但我看不到使用New重新初始化对象的任何地方。另外,暂时忘记myCmd.Dispose问题,您不会重置myCmd参数集合。这样,执行的命令的参数集合就错误了,也不要忘记打开插入零件的连接。(并关闭两个部件的开关)

您可以轻松避免使用不必要的全局变量

....
If Duplicate() = False Then
    STRSQL = "insert into glossary values (@word, @def)"
    Using myCmd = new MySqlCommand(STRSQL, myConn)
        myConn.Open()
        myCmd.Parameters.AddWithValue("word", txtNew.Text)
        myCmd.Parameters.AddWithValue("def", txtdefine.Text)
        myCmd.ExecuteNonQuery()
    End Using
    myConn.Close()
.....
Else
    STRSQL = "Update glossary set word = @term, definition = @mean where word = @term"
    Using myCmd = new MySqlCommand(STRSQL, myConn)
        myConn.Open()
        myCmd.Parameters.AddWithValue("term", txtNew.Text)
        myCmd.Parameters.AddWithValue("mean", txtdefine.Text)
        myCmd.ExecuteNonQuery()
    End Using
    myConn.Close()
.....
更好的解决方案是更改Connect()方法以返回初始化的连接,而不是使用全局变量。通过这种方式,您还可以在Using语句中包含连接的创建和销毁

 Using myConn as MySqlConnection = Connect()
 .......
 End Using
要使其正常工作,您需要以这种方式更改Connect的代码

Public Function Connect() as MySqlConnection
    Dim myConn As MySqlConnection
    myConnectionString = "Database=firstaidcqs;Server=localhost;Uid=root;Password="
    myConn = New MySqlConnection(myConnectionString)
    myConn.Open()
    return myConn
End Sub

无需
断开连接
功能,因为您将始终使用将为您关闭连接的,无需使用全局变量来保持连接,因为您将在每次需要时重新打开连接,然后关闭。不要认为这是因为ADO.NET实现了(这是一篇针对SqlServer的MSDN文章,但这个概念也适用于MySql)

耶!!我现在开始工作了:D(抱歉,我花了很长时间才完成这件事……我还在学习)。这是我的最终代码:

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Call Connect()
    If Blank() = False Then
        If Duplicate() = False Then
            STRSQL = "insert into glossary values (@word, @def)"
            Using myCmd = New MySqlCommand(STRSQL, myConn)
                myConn.Open()
                myCmd.Parameters.AddWithValue("word", txtNew.Text)
                myCmd.Parameters.AddWithValue("def", txtdefine.Text)
                myCmd.ExecuteNonQuery()
            End Using
            myConn.Close()
            MsgBox("Record Added")
            Dim word As String
            word = txtNew.Text
            lstword.Items.Add(word)
            myConn.Close()
        Else
            STRSQL = "Update glossary set word = @term, definition = @mean where word = @term"
            Using myCmd = New MySqlCommand(STRSQL, myConn)
                myConn.Open()
                myCmd.Parameters.AddWithValue("term", txtNew.Text)
                myCmd.Parameters.AddWithValue("mean", txtdefine.Text)
                myCmd.ExecuteNonQuery()
            End Using
            myConn.Close()
            MsgBox("Record Updated", MsgBoxStyle.Information, "New word added")
            Dim str As String
            str = txtNew.Text
            myConn.Close()
        End If
    End If
End Sub

Public Function Blank() As Boolean
    Call Connect()
    With Me
        If .txtNew.Text = "" Or .txtdefine.Text = "" Then
            Blank = True
            MsgBox("Cannot save! Term and definition should not contain null value", MsgBoxStyle.Critical, "Unable to save")
        Else
            Blank = False
        End If
    End With
End Function

Public Function Duplicate() As Boolean
    Dim dset As New DataSet
    Call Connect()
    With Me
        STRSQL = "Select * from glossary where word = '" & txtNew.Text & "'"
        myCmd.Connection = myConn
        myCmd.CommandText = STRSQL
        myAdptr.SelectCommand = myCmd
        myAdptr.Fill(dset, "glossary")
        myDataTable = dset.Tables("glossary")
        If myDataTable.Rows.Count > 0 Then
            Duplicate = True
            'MsgBox("Word already exist. Please check the word.", MsgBoxStyle.Critical, "Duplicate.")

        Else
            Duplicate = False
        End If
        myConn.Close()
    End With
End Function

我仍然使用我的第一个模块,但是我已经删除了Disconnect()函数@史蒂夫-非常感谢你的帮助,先生,我会尝试使用你给我的建议…也许在我的下一个节目。上帝的速度!!:)

什么版本的SQL Server?还有,你听说过SQL注入吗?你有什么错误吗?发生的事情是,当从列表中选择一个单词时,该单词和定义将显示并可以更新。这已经没有问题了。问题是,当我尝试插入新记录时,它会在
myCmd.Parameters.AddWithValue(“term”,txtNew.Text)
部分上显示“参数”term“已经定义”。似乎当我试图添加另一个记录时,最后选择的单词仍然是打开的,这就是为什么会发生这种情况…但这只是一个猜测。我应该怎么做才能在已经选择单词的情况下插入新记录?顺便说一句,我使用了mysql-5.5.27。另外,如果在从列表中选择任何记录之前先插入新记录,它会显示“记录已更新”,而不是“记录已添加”。然后,当我检查列表和表时,什么也没发生:(
使用myConn作为MySqlConnection=Connect()<代码> >我有一个错误,表示“表达式不产生值”。我看不到连接的代码。请将您的问题添加到上面的问题中。在该方法中,您应该返回一个MySqLink类型的对象,而不使用全局变量。如果@史提夫的答案是有用的,请考虑进行投票和/或接受它。感谢你的帮助。从长远来看,这会让你的问题更有可能得到答案。
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Call Connect()
    If Blank() = False Then
        If Duplicate() = False Then
            STRSQL = "insert into glossary values (@word, @def)"
            Using myCmd = New MySqlCommand(STRSQL, myConn)
                myConn.Open()
                myCmd.Parameters.AddWithValue("word", txtNew.Text)
                myCmd.Parameters.AddWithValue("def", txtdefine.Text)
                myCmd.ExecuteNonQuery()
            End Using
            myConn.Close()
            MsgBox("Record Added")
            Dim word As String
            word = txtNew.Text
            lstword.Items.Add(word)
            myConn.Close()
        Else
            STRSQL = "Update glossary set word = @term, definition = @mean where word = @term"
            Using myCmd = New MySqlCommand(STRSQL, myConn)
                myConn.Open()
                myCmd.Parameters.AddWithValue("term", txtNew.Text)
                myCmd.Parameters.AddWithValue("mean", txtdefine.Text)
                myCmd.ExecuteNonQuery()
            End Using
            myConn.Close()
            MsgBox("Record Updated", MsgBoxStyle.Information, "New word added")
            Dim str As String
            str = txtNew.Text
            myConn.Close()
        End If
    End If
End Sub

Public Function Blank() As Boolean
    Call Connect()
    With Me
        If .txtNew.Text = "" Or .txtdefine.Text = "" Then
            Blank = True
            MsgBox("Cannot save! Term and definition should not contain null value", MsgBoxStyle.Critical, "Unable to save")
        Else
            Blank = False
        End If
    End With
End Function

Public Function Duplicate() As Boolean
    Dim dset As New DataSet
    Call Connect()
    With Me
        STRSQL = "Select * from glossary where word = '" & txtNew.Text & "'"
        myCmd.Connection = myConn
        myCmd.CommandText = STRSQL
        myAdptr.SelectCommand = myCmd
        myAdptr.Fill(dset, "glossary")
        myDataTable = dset.Tables("glossary")
        If myDataTable.Rows.Count > 0 Then
            Duplicate = True
            'MsgBox("Word already exist. Please check the word.", MsgBoxStyle.Critical, "Duplicate.")

        Else
            Duplicate = False
        End If
        myConn.Close()
    End With
End Function