I have the store procedure for validating user id and password like this
PROCEDURE [dbo].[CheckLoginByUserIdAndPassword2]
@UserId char(15),
@Password char(40)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
select Name, UserPersonalId, UserId from MSUSERPERSONALINFO
where UserId = @UserId and Password=@Password
END
but , any idea what kind of code that should in my c# (windowsform application) to access this store procedure and receive the return value whether the login is successful or failed one?
I have made my code for calling that procedure , but still I cant set the return value..
try
{
string sp_name = "dbo.CheckLoginByUserIdAndPassword2";
SqlConnection SqlCon = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=jdwlrc_db;Data Source=.");
SqlCon.Open();
SqlCommand SqlCom = new SqlCommand(sp_name, SqlCon);
SqlCom.CommandType = CommandType.StoredProcedure;
SqlCom.Parameters.Add(new SqlParameter("@UserID",nama));
SqlCom.Parameters.Add(new SqlParameter("@Password",password));
SqlCom.ExecuteScalar();
SqlCon.Close();
return true;
}
catch
{
return false;
}
thx a lot..
I would suggest you to creat a seperate project( a class library) to do all your database communication. That should be our data access layer. your windows application should be some kind of thin client where it is not directly connected to the database. Then In your windows project, you add a reference to the classlibrary we created. Once reference is added, you should be able to access functions in the Class library from your UI project.
When use enter the username and password and click on login button, you read the values from the text box and make a call to the function in class library by passing the relevant data (in this case the user name and password). Get the response and decide what to do next.
Regarding the data access code, I am always inclined to wrap my code inside
usingstatements so that i dont need to worry about whether my connection is still open or not.