使用 VB.NET 更新 SQL 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39780795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:04:52  来源:igfitidea点击:

Update SQL database using VB.NET

vb.netsqlcommand

提问by Danys Chalifour

The code throw no errors it just didn't Execute the satement

代码没有抛出错误,只是没有执行语句

    Private Sub Update_Program(item As Programme)
    'Set Command
    SchoolTypes.Connexion.Open()
    item.Command = New SqlClient.SqlCommand("UPDATE T_Programme Set  pro_nom='@nom' , pro_nbr_unites=@nom , pro_nbr_heures=@unit WHERE pro_no ='@no'", SchoolTypes.Connexion)

    ''Add Parameter
    item.Command.Parameters.Add("@no", SqlDbType.VarChar, 6)
    item.Command.Parameters.Add("@nom", SqlDbType.VarChar, 50)
    item.Command.Parameters.Add("@unit", SqlDbType.Float)
    item.Command.Parameters.Add("@heures", SqlDbType.Int)
    ''''Set Values
    item.Command.Parameters("@no").Value = item.Pro_No
    item.Command.Parameters("@nom").Value = item.Pro_Nom
    item.Command.Parameters("@unit").Value = item.Pro_Nbr_Unit
    item.Command.Parameters("@heures").Value = item.Pro_Nbr_Heure

    Try
        If item.Command.ExecuteNonQuery() > 0 Then
            MsgBox("Modifier avec Succes!")
        End If
        SchoolTypes.Connexion.Close()
    Catch ex As Exception
        err.ShowDetails(System.Reflection.MethodBase.GetCurrentMethod(), ex)
    End Try
End Sub

I have tested my Command and it works on sql but not on the program...

我已经测试了我的命令,它适用于 sql 但不适用于程序...

Here's a paste of my database Database

这是我的数据库数据库的粘贴

回答by Pavan Chandaka

Your command statement is wrong. You should not give "''" marks for your parameters in update statement.

你的命令语句是错误的。您不应在更新语句中为您的参数提供“''”标记。

And also you have mismatch inputs. Below should be your update statement.

而且你还有不匹配的输入。下面应该是您的更新声明。

"UPDATE T_Programme Set pro_nom=@nom , pro_nbr_unites=@unit , pro_nbr_heures=@heures WHERE pro_no =@no"

“更新 T_Programme 设置 pro_nom=@nom , pro_nbr_unites=@unit , pro_nbr_heures=@heures WHERE pro_no =@no”

I tried below code. And it works fine.

我试过下面的代码。它工作正常。

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    Dim rowsAffected As Integer

    Using con As New SqlConnection("server=.;database=Test;integrated security=true")
        Using cmd As New SqlCommand("UPDATE T_Programme Set  pro_nom=@nom , pro_nbr_unites=@unit , pro_nbr_heures=@heures WHERE pro_no =@no", con)

            cmd.Parameters.Add("@no", SqlDbType.VarChar).Value = "1234"
            cmd.Parameters.Add("@nom", SqlDbType.VarChar).Value = "qwerty"
            cmd.Parameters.Add("@unit", SqlDbType.Float).Value = 12.0
            cmd.Parameters.Add("@heures", SqlDbType.Int).Value = 2

            con.Open()
            rowsAffected = cmd.ExecuteNonQuery()

        End Using
    End Using

End Sub