Can somebody tell me how to authenticate users with the SQL Server database table using VB as a web service. I managed to connect to the SQL server via this VB code;
Public Sub ConnectToSQL()
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = "Data Source=(local);Initial Catalog=TestDatabase;Persist Security Info=True;User ID=sa;Password=421"
con.Open()
Catch ex As Exception
MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
Finally
con.Close() 'Whether there is error or not. Close the connection.
End Try
End Sub
Any one ?Thank you in advance.
I would recommend that you first go through basic tutorials on SQL server as well as ADO.Net and then post questions. This will help in making your concepts clear as well as any help can be provided in more focused, problem areas. BTW, no hard feelings, just want you to be comfortable on programming. 🙂
Step 1 :
Create relevant database tables. These may include a
userstable and other tables such asroles, etc…EDIT : You can use SQL scripts to create tables in DB. Please find a sample below.
Please note that this sample is copied from http://sqlserverperformance.wordpress.com/2007/09/27/sample-table-and-index-creation-script-for-sql-server-2005/ and all credit related to its content goes to the author who wrote it.
Step 2 :
Write SQL query to find a user based on his
usernameandpassword. You may go for password encryption, but that is out of scope for current question.EDIT : Sample query can be as follows :
You can read through
SqlCommanddocumentation here to understand how to add parameters to a query.Step 3 :
Add
WebMethods so that you can createusersrecords in database. UseSqlCommandto fireINSERTqueries on your database tables.EDIT : Read here for a sample on how to write web services in VB.Net. And yes, you already know now, how to use
SqlCommandto fire SQL queries, do the same forINSERTqueries too.Step 4 :
Create a
WebMethod, that validates a user. For this, useSqlCommandobject to fire your query written in Step 2. If you get a row as a result, the user is valid.EDIT : Refer instructions to above steps and you should be able to create this on your own.
Hope I am clear enough.