Answered How to use VB with an SQL server

  • Monday, September 29, 2008 2:50 PM
     
     
    Hello.  I am new to using SQL and some what new to using VB but i need help with an application i am working on.  I have an SQL database that will have user names and passwords that i want my VB program to retrieve.  How can i pull out the data i need in the database and compare it to the data that the user just input to the program?

    Thanks for your help.

    Pierre 

All Replies

  • Tuesday, September 30, 2008 12:14 AM
     
     Answered
  • Tuesday, September 30, 2008 8:48 AM
     
     Answered
    Hello Turc,
             Please check out the how do I videos.
             How Do I in Visual Basic

    Waleed El-Badry Teaching Assistant Faculty of Engineering Misr University for Science & Technology
  • Thursday, October 02, 2008 6:41 AM
     
     Answered Has Code
    Turc said:

    I have an SQL database that will have user names and passwords that i want my VB program to retrieve.  How can i pull out the data i need in the database and compare it to the data that the user just input to the program?



    Thank you All for your friendly help.

    Hi Pierre,

    Welcome to MSDN forums!

    Please check and try the following code samples.

    Code sample 1:

    Imports System.Data.SqlClient  
    Public Class Form1  
     
        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
            Dim con As New SqlConnection("Data Source=.;Integrated Security=True;AttachDbFilename=D:\MyDB.mdf")  
            Dim cmd As SqlCommand = New SqlCommand("SELECT UserName,UserPassword FROM UserTable", con)  
            con.Open()  
            Dim sdr As SqlDataReader = cmd.ExecuteReader()  
     
            While sdr.Read = True 
                If sdr.Item("UserName") = TextBox1.Text Then 
                    MessageBox.Show("The user exists in database!")  
                End If 
            End While 
            sdr.Close()  
            con.Close()  
     
        End Sub 
     
    End Class 

    Code sample 2:
    Imports System.Data.SqlClient  
    Public Class Form1  
     
        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
            Dim con As New SqlConnection("Data Source=.;Integrated Security=True;AttachDbFilename=D:\MyDB.mdf")  
            Dim cmd As SqlCommand = New SqlCommand("SELECT UserName,UserPassword FROM UserTable WHERE UserName = '" & TextBox1.Text & "'", con)  
            con.Open()  
            Dim sdr As SqlDataReader = cmd.ExecuteReader()  
     
            If sdr.Read() = True Then 
                MessageBox.Show("The user exists in database!")  
            End If 
     
            sdr.Close()  
            con.Close()  
     
        End Sub 
     
    End Class 


    Walkthrough: How to make a Login Verification routine (Login Form)?
    http://social.msdn.microsoft.com/forums/en/vbgeneral/thread/4eb4dc84-a62f-4a46-95b0-3b981fb83f23/



    Best regards,
    Martin Xie