add rows to datagridview

1

How can I declare the @NIF as a string

Dim sqlConString As String = "Server=localhost\TESTE;Database=tempTest;User Id=sa;Password=123"
    Dim conn = New SqlConnection(sqlConString)
    Try
        Conn.Open()
        Dim strSQL As String = "SELECT * From Computador Where Nifcliente = @NIF"
        conn.Close()
        Dim da As New SqlDataAdapter(strSQL, Conn)
        Dim ds As New DataSet
        da.Fill(ds, "Computador")
        DataGrid.DataSource = ds.Tables(0)
    Catch ex As SqlException
        MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
    End Try
    
asked by anonymous 09.05.2018 / 13:28

2 answers

0

You can do the following:

Dim strSQL As String = string.format("SELECT * From Computador Where Nifcliente = {0)",NIF)

It would even be better:

        Dim sqlConString As String = "Server=localhost\TESTE;Database=tempTest;User Id=sa;Password=123"
        Dim conn = New SqlConnection(sqlConString)

        Try
            Conn.Open()

            Dim strSQL As String = "SELECT * From Computador Where Nifcliente = @NIF"
            Dim cmd As New SqlClient.SqlCommand(strSQL, Conn)

            cmd.Parameters.Add(New SqlClient.SqlParameter("@NIF", ValorDeNIF))

            Dim da As New SqlDataAdapter(cmd)
            Dim ds As New DataSet

            da.Fill(ds, "Computador")

            conn.Close()

            DataGrid.DataSource = ds.Tables(0)

        Catch ex As SqlException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
        End Try
    
09.05.2018 / 14:06
0
Dim sqlConString As String = "Server=localhost\TESTE;Database=tempTest;User Id=sa;Password=123"
    Dim conn = New SqlConnection(sqlConString)
    Try
        conn.Open()
        Dim strSQL As String = "SELECT * From Computador Where Nifcliente = '" + clientNIF + "'"
        conn.Close()
        Dim da As New SqlDataAdapter(strSQL, conn)
        Dim ds As New DataSet
        da.Fill(ds, "Computador")
        DataGrid.DataSource = ds.Tables(0)
    Catch ex As SqlException
        MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
    End Try
    
09.05.2018 / 17:24