How to connect Visual studio with Sql Server
The connection string that includes the
source database name, and other parameters needed to establish the
initial connection. The default value is an empty string.
Implements
Lets Go......
Requirment : Create Login Page in Asp.Net
Ans: Open Visual studio
- Create an asp.net project to check login of a user
- create2 label, 2 Text box and a button
STEP 1
database name : db_user
table name : tbl_user
Open visual studio >> create an asp application >> Right click on the project to add a class file ,name"Utility"
Open "Utiity" class and write below code.......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
namespace UI
{
public class Utility
{
private int id;
private string name;
private string userName;
private string password;
public int Id
{
set { this.id = value; }
get { return this.id; }
}
public string Name
{
set { this.name = value; }
get { return this.name; }
}
public string UserName
{
set { this.userName = value; }
get { return this.userName; }
}
public string Password
{
set { this.password = value; }
get { return this.password; }
}
SqlConnection con = new SqlConnection("Data Source=10.18.184.133;Initial Catalog=db_user;User ID=niituser;Password=niituser");
SqlCommand cmd;
SqlDataReader reader;
public bool Login(Utility emp)
{
bool status = false;
try
{
con.Open();
cmd = new SqlCommand("sp_login", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@username", emp.userName);
cmd.Parameters.AddWithValue("@password", emp.password);
cmd.Parameters.AddWithValue("@return", DbType.Int32).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
Int32 val = Convert.ToInt32(cmd.Parameters["@return"].Value);
if (val == 1)
{
status = true;
}
}
catch (Exception)
{
}
finally
{
con.Close();
con.Dispose();
}
return status;
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
namespace UI
{
public class Utility
{
private int id;
private string name;
private string userName;
private string password;
public int Id
{
set { this.id = value; }
get { return this.id; }
}
public string Name
{
set { this.name = value; }
get { return this.name; }
}
public string UserName
{
set { this.userName = value; }
get { return this.userName; }
}
public string Password
{
set { this.password = value; }
get { return this.password; }
}
SqlConnection con = new SqlConnection("Data Source=10.18.184.133;Initial Catalog=db_user;User ID=niituser;Password=niituser");
SqlCommand cmd;
SqlDataReader reader;
public bool Login(Utility emp)
{
bool status = false;
try
{
con.Open();
cmd = new SqlCommand("sp_login", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@username", emp.userName);
cmd.Parameters.AddWithValue("@password", emp.password);
cmd.Parameters.AddWithValue("@return", DbType.Int32).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
Int32 val = Convert.ToInt32(cmd.Parameters["@return"].Value);
if (val == 1)
{
status = true;
}
}
catch (Exception)
{
}
finally
{
con.Close();
con.Dispose();
}
return status;
}
}
}
No comments:
Post a Comment