Desktop application database connectivity
Today we will learn how to connect database in desktop application. For a reference you can also follow Basic Login website with database connectivity which will help you in understanding database basic handling.
I am repeating the same project and also you can download the project. The link is at the end of the page.
The sql query for the project is:
CREATE TABLE [dbo].[tblUser](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[username] [varchar](50) NULL,
[password] [varchar](50) NULL
insert into dbo.tblUser(username,password) values (‘vipin’,’mishra’)
insert into dbo.tblUser(username,password) values (‘user’,’pass’)
If you dont have mydatabase you can create it by using sql query :
create database mydatabase
Now design the form as looks like in the image:
Now click on the login button and write down the code as below.
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(“Data Source=.\\sqlexpress;Initial Catalog=mydatabase;Integrated Security=True”);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = “Select * from dbo.tblUser”;
SqlDataAdapter da = new SqlDataAdapter(cmd);
string UserName = txtUsername.Text.ToString();
string Password = txtPassword.Text.ToString();
if (dt != null && dt.Rows.Count > 0)
UserName = dt.Rows[“username”].ToString();
Password = dt.Rows[“password”].ToString();
if (txtUsername.Text.Trim() == UserName && txtPassword.Text.Trim() == Password)
MessageBox.Show(“wrong username and password combination!!”);
Download the project desktop login project with sql connectivity .
How useful was this post?
Click on a star to rate it!
Average rating / 5. Vote count:
We are sorry that this post was not useful for you!
Let us improve this post!
Thanks for your feedback!