For Programmers: Free Programming Magazines  


Home > Archive > C# > December 2005 > Storde procedure Guidlines?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Storde procedure Guidlines?
aua_usama@yahoo.com

2005-12-12, 4:13 am

Dear All:
i just wanna know if anyone have an ideas or guidlines on How to call
an stored Proc in C# windows application;
samples will help also very much
regards

Razor

2005-12-12, 10:10 pm

something like the following should do it

using System;
using System.Data;
using System.Data.SqlClient;

namespace test
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class test
{
public bool IsValidUser(SqlConnection cndata , int intApplicationId,
string strUserId, string strPassword, ref string strMsg, ref bool
bValidUser)
{
try
{
// ****************************************
*******************************
//connect to the database using an existing connection
// ****************************************
*******************************
SqlCommand spCommand = new SqlCommand("SP_SEC_LOGIN", cndata);
spCommand.CommandType = CommandType.StoredProcedure;

// ****************************************
*******************************
//add the values for the specific parameters
// ****************************************
*******************************
SqlParameter param1 = new SqlParameter("@AppId", SqlDbType.Int);
param1.Direction = ParameterDirection.Input;
param1.Value = intApplicationId;
spCommand.Parameters.Add(param1);

SqlParameter param2 = new SqlParameter("@UserId",
SqlDbType.VarChar);
param2.Direction = ParameterDirection.Input;
param2.Value = strUserId;
spCommand.Parameters.Add(param2);

SqlParameter param3 = new SqlParameter("@Password",
SqlDbType.VarChar);
param3.Direction = ParameterDirection.Input;
param3.Value = strPassword;
spCommand.Parameters.Add(param3);

// ****************************************
*******************************
//Notice the type of results we expect from this paramerter
// ****************************************
*******************************
SqlParameter param4 = new SqlParameter("@ERROR_TYPE",
SqlDbType.Int);
param4.Direction = ParameterDirection.InputOutput;
param4.Value = 0;
spCommand.Parameters.Add(param4);

// ****************************************
*******************************
//instead of using .ExecuteNonQuery()
//you could just use
//WhateverDataset.Fill(dsPermissions)
//if you are expecting results from the Stored Procedure
// ****************************************
*******************************

spCommand.ExecuteNonQuery();

bValidUser =
System.Convert.ToInt32(spCommand.Parameters["@ERROR_TYPE"].Value) ==
-1;
if (!(bValidUser))
strMsg = "Stored Procedure failed";
return bValidUser;
}
catch (Exception ex)
{
strMsg = "IsValidUser - " + ex.Message + " AppId : " +
System.Convert.ToString(intApplicationId) + ", UserName: " + strUserId
+ ", Password: " + strPassword;
return false;
}
}

}
}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com