I'm new to C# I need to call a SP and return the table value from C#.
I'm passing current datetime(#currentdate) as my input parameter in the stored procedure.So how do I pass this in C# method?
Pls help me to write a C# method to call that SP and return the value
I have my sp ready. sp will return the top updated record in the table.
I have used this in my C# code
string _Num = null;
SqlConnection sqlConnection1 = new SqlConnection("Data Source=localhost;Initial Catalog=ReferenceDB;Persist Security Info=True;Integrated Security=SSPI");
SqlCommand cmd = new SqlCommand();
Int32 rowsAffected;
Object returnValue;
cmd.CommandText = "Number";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#CurrentDate", DateTime.Now);
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
rowsAffected = cmd.ExecuteNonQuery();
returnValue = cmd.ExecuteScalar();
if (returnValue != null)
_Num = returnValue.ToString();
return _Num
Write his code in that method
SqlConnection con = new SqlConnection("coonectionstring");
SqlCommand cmd = new SqlCommand();
SqlCommand cmd = new SqlCommand("Your sp",con);
cmd.CommandType = CommandType.StoredProcedure;
//input parameters
cmd.Parameters.Add(new SqlParameter("#currentdate",SqlDbType.DateTime,"currentdate"));
int i=command.ExecuteNonQuery();
If you are going to use more than one sp in your C# code, I suggest something like this:
public static DataTable GetSQLValues(string currentDate)
{
SqlParameter[] sqlParams =
{
new SqlParameter("#currentDate", currentDate)
};
return DBHelper.GetTable("MSSQLSPNAME", sqlParams);
}
Use the above for every instance where you need to get table info.
GetTable looks like this:
static SqlConnection _conn;
static string _connStr = ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["primaryConnStr"]].ToString();
static SqlCommand _cmd;
static SqlDataAdapter _dataAdapter = new SqlDataAdapter();
public static DataTable GetTable(string sProcName, SqlParameter[] sqlParams)
{
using (_conn = new SqlConnection(_connStr))
{
_cmd = new SqlCommand(sProcName, _conn);
_conn.Open();
_cmd.CommandType = CommandType.StoredProcedure;
if (sqlParams != null)
{
_cmd.Parameters.AddRange(sqlParams);
}
_dataAdapter = new SqlDataAdapter(_cmd);
var results = new DataTable();
_dataAdapter.Fill(results);
_conn.Close();
return results;
}
}
It will save you a ton of time for each sp call you need from C#.
Related
I have the following function in C# which is working fine
private void AddQueue()
{
SqlConnection conn = forconnection();
conn.Open();
SqlCommand cmd = new SqlCommand("spInsertFormIssue",conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#Qdatetime", SqlDbType.DateTime).Value = DateTime.Now;
cmd.ExecuteNonQuery();
conn.Close();
}
Now I want the same function but with different stored procedure, I want to reuse this function with another stored procedure.
How do I pass stored procedure as argument?
You can get the stored procedure name and parameters from request argument.
Like this:
private void AddQueue(string spName, List<SqlParameter> SqlParameters)
{
...
SqlCommand cmd = new SqlCommand(spName, conn);
...
if (SqlParameters.Count > 0)
cmd.Parameters.AddRange(SqlParameters.ToArray());
...
}
And you can call it like this:
List<SqlParameter> sqlParameters = new List<SqlParameter>();
sqlParameters.Add(new SqlParameter("#Qdatetime", SqlDbType.DateTime) { Value = DateTime.Now });
AddQueue("spInsertFormIssue", sqlParameters);
Just pass name of the stored procedure as parameter:
string procedureName = "spInsertFormIssue";
private void AddQueue(string procedureName)
{
SqlConnection conn = forconnection();
conn.Open();
SqlCommand cmd = new SqlCommand(procedureName,conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#Qdatetime", SqlDbType.DateTime).Value = DateTime.Now;
cmd.ExecuteNonQuery();
conn.Close();
}
I want to return a double value from a stored procedure so I can call it from a form and multiplication its value in the value of the text box.
My stored procedure looks like this:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Get_Weight]
#ID INT,
#Weight FLOAT OUTPUT
AS
SELECT #Weight = [Weight]
FROM [Item]
WHERE ID_Item = #ID
This is my data access layer class:
class DataAccessLayer
{
SqlConnection sqlconnection;
public DataAccessLayer()
{
PL.FRM_LOGIN frm = new PL.FRM_LOGIN();
sqlconnection = new SqlConnection("Server='"+System.Environment.MachineName+"';DataBase='"+frm.txtDataBase.Text+"';Integrated Security=true");
}
// Method to open the connection
public void Open()
{
if(sqlconnection.State != ConnectionState.Open)
{
sqlconnection.Open();
}
}
// Method to close the connection
public void Close()
{
if(sqlconnection.State == ConnectionState.Open)
{
sqlconnection.Close();
}
}
// Method to read data from database
public DataTable SelectData(string stored_procedure, SqlParameter[] param)
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = stored_procedure;
sqlcmd.Connection = sqlconnection;
if (param != null)
{
sqlcmd.Parameters.AddRange(param);
}
SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
// Method to insert, update and delete data from database
public void ExecuteCommand(string stored_procedure, SqlParameter[] param)
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = stored_procedure;
sqlcmd.Connection = sqlconnection;
if (param != null)
{
sqlcmd.Parameters.AddRange(param);
}
sqlcmd.ExecuteNonQuery();
}
}
I want to create a class in the business layer with a method that can return the value - for example
public void Get_Weight(int ID, double UWeight)
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DAL.Open();
SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter("#ID", SqlDbType.Int);
param[0].Value = ID;
param[1] = new SqlParameter("#Weight", SqlDbType.Float);
param[1].Value = UWeight;
param[1].Direction = ParameterDirection.Output;
DAL.ExecuteCommand("Get_Weight", param);
DAL.Close();
}
And after that, I call that method from the form
void CalculateWeight()
{
if (txtLength.Text != string.Empty && cmbName.Text != null)
{
txtWeight.Text = (Convert.ToInt32(txtLength.Text) *(//the code)).ToString();
}
}
Please help me
If this were a code review I would have a number of issues with your approach to a Data Access Layer, but to solve your immediate problem I would suggest you change your Get_Weight method to return a double and not take a UWeight argument. Since your OUTPUT parameter is only set and not also used as input, you can give it a value of DBNull.Value. Lastly, it looks like you might have a typo in your procedure, is the column name really "Wight"?
public double Get_Weight(int ID)
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DAL.Open();
SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter("#ID", SqlDbType.Int);
param[0].Value = ID;
param[1] = new SqlParameter("#Weight", SqlDbType.Float);
param[1].Value = DBNull.Value;
param[1].Direction = ParameterDirection.Output;
DAL.ExcuteCommande("Get_Weight", param);
DAL.Close();
double weight = 0.0;
if(double.TryParse(param[1]?.Value?.ToString(), out weight)
{
return weight;
}
else
{
throw new ArgumentException("No Item found for given ID");
}
}
Use ExecuteScalar to get a value from a stored procedure
public double ExcuteCommande(string stored_procedure,SqlParameter[] param)
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = stored_procedure;
sqlcmd.Connection = sqlconnection;
if (param!=null)
{
sqlcmd.Parameters.AddRange(param);
}
var back=sqlcmd.ExecuteScalar();
double result;
double.TryParse(back.ToString(), out result);
return result;
}
from my application I am retrieving data using ADO.Net classes.
private DataSet GetData(string clientconstr,string actionparam,string userid)
{
DataSet dsData = null;
SqlParameter[] objSqlParam = new SqlParameter[2];
objSqlParam[0] = new SqlParameter("#ACTION", SqlDbType.VarChar, 50);
objSqlParam[0].Value = actionparam;
objSqlParam[1] = new SqlParameter("#USERID", SqlDbType.VarChar, 100);
objSqlParam[1].Value = userid;
dbc = new dbClass(clientconstr);
dsData = dbc.ExecuteNonQuery("SPLINV", "SP", objSqlParam);
}
class dbClass
{
SqlCommand cmd = null;
SqlConnection con = null;
SqlDataAdapter da = null;
string connectionstring = "";
public dbClass(string conStr)
{
connectionstring = conStr;
}
public DataSet ExecuteNonQuery(string query, string querytype, SqlParameter[] objArrSqlParamas)
{
DataSet ds = null;
try
{
con = new SqlConnection(connectionstring);
con.Open();
cmd = new SqlCommand();
cmd.Connection = con;
if (querytype.Equals("sp"))
cmd.CommandType = CommandType.StoredProcedure;
else
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
cmd.CommandTimeout = 300;
if (objArrSqlParamas != null)
{
for(int idx=0;idx<objArrSqlParamas.Length;idx++)
cmd.Parameters.Add(objArrSqlParamas[idx]);
}
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (cmd != null)
cmd.Dispose();
if (da != null)
da.Dispose();
if (con != null)
{
con.Dispose();
con.Close();
}
}
return ds;
}
}
Even though I am passing all the parameters to SP "SPLINV",I am getting below error in the GetData function.
System.Data.SqlClient.SqlException: Procedure or function 'SPLINV'
expects parameter '#ACTION', which was not supplied.
"SPLINV" Sp was created like below.
CREATE PROCEDURE SPLINV
(
#ACTION VARCHAR(50),
#USERID VARCHAR(100)
)
I just want to know why this issue happening even though I am passing all the parameters to SP and how to resolve this.
The problem is here :- if (querytype.Equals("sp"))
Equals is case sensitive. You are passing "SP" and checking for "sp". So the command get executed as text. Please go through the changed code.
private DataSet GetData(string clientconstr,string actionparam,string userid)
{
DataSet dsData = null;
SqlParameter[] objSqlParam = new SqlParameter[2];
objSqlParam[0] = new SqlParameter("#ACTION", SqlDbType.VarChar, 50);
objSqlParam[0].Value = actionparam;
objSqlParam[1] = new SqlParameter("#USERID", SqlDbType.VarChar, 100);
objSqlParam[1].Value = userid;
dbc = new dbClass(clientconstr);
dsData = dbc.ExecuteNonQuery("SPLINV", "SP", objSqlParam);
}
class dbClass
{
SqlCommand cmd = null;
SqlConnection con = null;
SqlDataAdapter da = null;
string connectionstring = "";
public dbClass(string conStr)
{
connectionstring = conStr;
}
public DataSet ExecuteNonQuery(string query, string querytype, SqlParameter[] objArrSqlParamas)
{
DataSet ds = null;
try
{
con = new SqlConnection(connectionstring);
con.Open();
cmd = new SqlCommand();
cmd.Connection = con;
if (querytype.Equals("SP"))
cmd.CommandType = CommandType.StoredProcedure;
else
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
cmd.CommandTimeout = 300;
if (objArrSqlParamas != null)
{
for(int idx=0;idx<objArrSqlParamas.Length;idx++)
cmd.Parameters.Add(objArrSqlParamas[idx]);
}
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (cmd != null)
cmd.Dispose();
if (da != null)
da.Dispose();
if (con != null)
{
con.Dispose();
con.Close();
}
}
return ds;
}
}
Have you tried setting the parameter directions to "Input"?
objSqlParam[0].Direction = ParameterDirection.Input;
objSqlParam[1].Direction = ParameterDirection.Input;
I have a Windows Forms program I was writing at home (I mostly work with ASP.Net so it's been a while) and I'm having trouble executing stored procedure commands.
I create the SqlCommand object from the SqlConnection object and then create the SqlParameter from the SqlCommand. I specify the name, data type, and so on. However, whenever I call SqlCommand ExecuteReader() it's telling me it expects parameters that were not provided. I clearly added them and can see them populated when stepping through in Debug. Any ideas?
Stored procedure:
EXEC dbo.GetTransactions #StartDate = '2015-04-10 18:07:43',
#EndDate = '2015-04-10 18:07:43'
Class DataAccess:
public static DataTable Execute(SqlCommand iCommand) {
DataTable objTable = new DataTable();
try {
iCommand.Connection.Open();
SqlDataReader objReader = iCommand.ExecuteReader();
objTable.Load(objReader);
objReader.Close();
}
catch {
throw;
}
finally {
iCommand.Connection.Close();
}
return objTable;
}
public static SqlCommand CreateCommand(string iProcedureName) {
try {
SqlConnection objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ToString());
SqlCommand objCommand = new SqlCommand(iProcedureName, objConnection);
return objCommand;
}
catch {
throw;
}
}
Class TransactionCollection:
private static DataTable Load(DateTime iStartDate, DateTime iEndDate) {
string strProcedureName = "GetTransactions";
SqlCommand objCommand = DataAccess.CreateCommand(strProcedureName);
SqlParameter param = objCommand.CreateParameter();
param.ParameterName = "#StartDate";
param.DbType = DbType.DateTime;
param.Value = iStartDate;
objCommand.Parameters.Add(param);
param = objCommand.CreateParameter();
param.ParameterName = "#EndDate";
param.DbType = DbType.DateTime;
param.Value = iEndDate;
objCommand.Parameters.Add(param);
return DataAccess.Execute(objCommand);
}
You need to tell your SqlCommand that it's executing a stored procedure! You need to set the CommandType of your SqlCommand - see here:
public static SqlCommand CreateCommand(string iProcedureName) {
try {
SqlConnection objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ToString());
SqlCommand objCommand = new SqlCommand(iProcedureName, objConnection);
// add this line here!!
objCommand.CommandType = CommandType.StoredProcedure;
return objCommand;
}
catch {
throw;
}
}
My code shown below is create as an inline SQL statement. How can this code be written as a stored procedure??
The code is:
public Stream SelectEmployeeImageByID(int theID)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString());
string sql = "SELECT Image FROM Employees WHERE EmployeeId = #EmployeeId";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#EmployeeId", theID);
connection.Open();
object theImg = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])theImg);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}
you can do this
create procedure SelectEmployeeImage(#employee int)
as
begin
SELECT Image FROM Employees WHERE EmployeeId = #EmployeeId
end
then your code will be this form
public Stream SelectEmployeeImageByID(int theID)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString());
string sql = "SelectEmployeeImage";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#EmployeeId", theID);
connection.Open();
object theImg = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])theImg);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}
hope this will help you
Creating Stored Procedure
Create procedure SP_InsertEmployee
as
#EmployeeId int
BEGIN
SELECT Image FROM Employees WHERE EmployeeId=#EmployeeId
END
You Should Set CommandType=StoredProcedure and Rest of will be same
cmd.CommandType = CommandType.StoredProcedure;
Recommendations
Always use using which automatically disposes connections
using (SqlConnection con = new SqlConnection())
{
con.open();
using (SqlCommand cmd = new SqlCommand(sql, connection))
{
//object theImg = cmd.ExecuteScalar();
}
con.Dispose();
}