Académique Documents
Professionnel Documents
Culture Documents
Step One: Add Service Based Database to your Project 1. Click Project, Choose Add New Item 2. Select Service-based Database 3. Give your Databse a name and Click Add (Database Name in Example is ApplicaionDB)
4. A Data Source Configuration will appear. Click Cancel, Since we have no Tables in the new Database
Page 1
5. Database you created will appear on your Server Explorer Tab, Under Data Connections. 6. Double Click on your Database (ApplicationDB.mdf)
Page 2
9. To Save the Table, Right Click on the Table Definition Tab and Save Table1
10. An Input Box will appear, prompting table name. Save table as Users
11. Right click on the Table Definition Tab and Close it(Click Close)
Page 3
Step Two: Get Database Connection String 1. Click Data, Choose Add New Data Source 2. A Data Source Configuration will appear. Click Database, then Next
Page 4
4. The Next Dialog box will show you a list on databases you have on your system, Choose the one your created (ApplicationDB.mdf) 5. On the same Dialog Box, The last section should be labeled Connection string 6. Click on the + sign to expand it, and your connection string will be displayed
7. Copy and save your Connection String. It should be something like this; Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ApplicationDB.mdf;Integrated Security=True;User Instance=True 8. Click Cancel, Since we didnt want to create any Datasets, We just wanted to get a Connection String
Page 5
Object Form
TextBox TextBox
Button
Button
Form
Page 6
Code
Public Class LoginForm ' TODO: Perform custom authentication using the provided username and password Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click Try Dim MyDbConnection As New SqlClient.SqlConnection Dim MySqlCommand As New SqlClient.SqlCommand Dim MyAdapter As New SqlClient.SqlDataAdapter Dim MyDataset As New DataSet 'Add Connection String (See Pages 4 and5 on how to get your connection string) MyDbConnection.ConnectionString = ("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ApplicationDB.mdf;Integrated Security=True;User Instance=True") MySqlCommand.CommandText = "SELECT * From [Users] WHERE UserName= '" & UsernameTextBox.Text & "' AND Password='" & PasswordTextBox.Text & "' " 'Open Connection and Query the MyDbConnection.Open() MySqlCommand.Connection = MyAdapter.SelectCommand = MyAdapter.Fill(MyDataset, Database MyDbConnection MySqlCommand "0")
'There Can only be one user with the Entered Username and Password Dim Count = MyDataset.Tables(0).Rows.Count If Count = 1 Then Main.Show() 'Form Show When Username and Password are Valid Me.Hide() Me.Visible = False PasswordTextBox.Clear() UsernameTextBox.Clear() Else MsgBox("Please Check Your Username and/or Password and Try Again", MsgBoxStyle.Critical, "Login Failed") PasswordTextBox.Clear() UsernameTextBox.Focus() End If MyDbConnection.Close() Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click Application.Exit() End Sub End Class
Page 7