Add new parameters to exec procedure - c#

I have a custom SQL exec to execute stored procedure and send DataTable and execute TableType in sql like:
//Execute
db.ExeSQLParam("usp_TaskStatus_Time_Calculation_Final", parameters, "#GuidIdTableType");
ExeSQLParam method:
public bool ExeSQLParam(string SprocName, DataTable paramArray, string tableTypeName)
{
var testc = new SqlParameter();
bool bFlag = false;
SqlCommand cmd = new SqlCommand(SprocName, this.dbconn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter(tableTypeName, SqlDbType.Structured));
cmd.Parameters[tableTypeName].Value = paramArray;
try
{
cmd.ExecuteNonQuery();
bFlag = true;
}
catch (SqlException e)
{
this.HandleSQLError(e, SprocName, paramArray.ToString());
}
finally
{
cmd.Dispose();
}
return bFlag;
}
My question is how can I add to this method another normal parameters , so I can execute as:
db.ExeSQLParam("usp_TaskStatus_Time_Calculation_Final", parameters, "#GuidIdTableType",
#anoteherParameter = 'valueanotherparameter', #other = 'valueoter');
How can I achieve that?
I try to change it to received output data as DataTable like:
public DataTable ExeSQLParamAndType(string SprocName, DataTable paramArray, string tableTypeName)
{
SqlCommand cmd = new SqlCommand(SprocName, this.dbconn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter(tableTypeName, SqlDbType.Structured));
cmd.Parameters[tableTypeName].Value = paramArray;
DataTable tbl = new DataTable("Table1")
{
Locale = System.Globalization.CultureInfo.InvariantCulture
};
SqlDataAdapter da = new SqlDataAdapter(cmd);
try
{
da.Fill(tbl);
}
catch (SqlException e)
{
this.HandleSQLError(e, "GetTableBySQL", SprocName);
}
finally
{
cmd.Dispose();
}
return tbl;
}
but where I can add new parameters? with another parameters.Add? I'm a litte confused there

In C# you can provide optional parameters using the 'params' keyword, so you simply rewrite your method and supply the params parametres.

Related

SqlDataAdapter filling with DataTable does not work

I have this code running in form_load event:
using (SqlConnection sqlConn = new SqlConnection(strConn))
{
sqlConn.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("pp_sp_MachineAndOp", sqlConn);
DataTable sqlDt = Helper.ExecuteDataTable("pp_sp_MachineAndOp", new SqlParameter("#MachineAndOpID", 7));
sqlDa.Fill(sqlDt);
dgvMachineAndOp.AutoGenerateColumns = false;
dgvMachineAndOp.DataSource = sqlDt;
sqlDa.Dispose();
sqlConn.Close();
}
I get error 'Procedure or function 'pp_sp_MachineAndOp' expects parameter '#MachineAndOpID', which was not supplied.' at line:
sqlDa.Fill(sqlDt);
Important to say that if I open DataTable Visualizer of sqlDt at runtime I see expected results!
Here is a code behind Helper.ExecuteDataTable:
public static DataTable ExecuteDataTable(string storedProcedureName, params SqlParameter[] arrParam)
{
DataTable dt = new DataTable();
// Open the connection
using (SqlConnection sqlConn = new SqlConnection(strConn))
{
try
{
sqlConn.Open();
// Define the command
using (SqlCommand sqlCmd = new SqlCommand())
{
sqlCmd.Connection = sqlConn;
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = storedProcedureName;
// Handle the parameters
if (arrParam != null)
{
foreach (SqlParameter param in arrParam)
{
sqlCmd.Parameters.Add(param);
}
}
// Define the data adapter and fill the dataset
using (SqlDataAdapter da = new SqlDataAdapter(sqlCmd))
{
da.Fill(dt);
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return dt;
}
What I am missing?
Remove everything except
DataTable sqlDt = Helper.ExecuteDataTable("pp_sp_MachineAndOp", new SqlParameter("#MachineAndOpID", 7));
dgvMachineAndOp.AutoGenerateColumns = false;
dgvMachineAndOp.DataSource = sqlDt;
your Helper.ExecuteDataTable is doing everything. you don't need to replicate same this in your code.
I think your helper class is creating connection with database as your data table has data.
So, try to remove stored proc name and connection object from adaptor and then check.
SqlDataAdapter sqlDa = new SqlDataAdapter();//use this only.
you can use below function(modification required as per your need):
public IDataReader ExecuteReader(string spName, object[] parameterValues)
{
command = GetCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = spName;
if (parameterValues != null)
{
for (int i = 0; i < parameterValues.Length; i++)
{
command.Parameters.Add(parameterValues[i]);
}
}
reader = command.ExecuteReader();
if (parameterValues != null)
command.Parameters.Clear();
return reader;
}

OracleParameterCollection giving me weird errors

Error CS1729 'OracleParameterCollection' does not contain a constructor that takes 0 arguments
My Code
OracleParameterCollection oracleParameter = new OracleParameterCollection(); <====== How do I create one?
oracleParameter.Add("User_Name", OracleDbType.Char).Value = UserName;
oracleParameter.Add("Entered_Password", OracleDbType.Char).Value = Password;
oracleParameter.Add("T1_Cursor", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
DataTable employeeDataTable = StoredProcedureCall.GenerateStoredProcedureCall(_connectionString, "GET_USER_INFO_BY_CREDENTIALS", oracleParameter, out temp);
Method in class
public DataTable GenerateStoredProcedureCall(String _connectionString, String StoredProcedure_Name, OracleParameterCollection ParameterNames, out String ResultFromDatabaseOperation)
{
DataTable dt = new DataTable();
try
{
using (OracleConnection cn = new OracleConnection(_connectionString))
{
OracleDataAdapter da = new OracleDataAdapter();
OracleCommand cmd = new OracleCommand();
cmd.Connection = cn;
cmd.CommandText = StoredProcedure_Name;
cmd.CommandType = CommandType.StoredProcedure;
cmd.BindByName = true;
cmd.Parameters.Add(ParameterNames);
da.SelectCommand = cmd;
da.Fill(dt);
ResultFromDatabaseOperation = "";
if (cmd.Parameters["RowCount"].Value != null)
{
ResultFromDatabaseOperation = cmd.Parameters["RowCount"].Value.ToString();
}
if (cmd.Parameters["RowCount"].Value == null)
{
ResultFromDatabaseOperation = "0";
}
return dt;
}
}
catch (OracleException ex)
{
ResultFromDatabaseOperation = "";
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine(ex.StackTrace.ToString());
return dt;
}
}
If StoredProcedureCall.GenerateStoredProcedureCall() is your own defined function, then You should not pass OracleParameterCollection list to it. You can create simple dictionary and pass to this function.
And then inside the function you can add parameter directly to command. i.e:
foreach(var i in params)
{
oraCommand.Parameters.Add(new OracleParameter(i.key, i.val));
}
There is no need to create new instance of OracleParameterCollection

sqlcommand c# method with sql paramather

I have this method, which I have in the base class that helps me to select anything from the children classes and also to reduce code repetition. The problem is when I call it I get an error which is a NullReferenceException (and when I look it up I find that the command in the method is empty).
This is the method in question:
This way I already know how to use but the other one I don't
SqlCommand command = new SqlCommand("select * from Customers where idCustomer=#idCustomer", OpenConnection());
command.Parameters.AddWithValue("#idCustomer", Id);
SqlDataReader reader = command.ExecuteReader();
Customer Onecustomer = null;
if (reader.Read())
{
Onecustomer = ReadCustomer(reader);
}
protected DataTable ExecuteSelectQuery(String query, params SqlParameter[] sqlParameters)
{
SqlCommand command = new SqlCommand();
DataTable dataTable;
DataSet dataSet = new DataSet();
try
{
command.Connection = OpenConnection();
command.CommandText = query;
command.Parameters.AddRange(sqlParameters);
command.ExecuteNonQuery();
adapter.SelectCommand = command;
adapter.Fill(dataSet);
dataTable = dataSet.Tables[0];
}
catch (SqlException e)
{
return null;
throw new Exception("Error :" + e.Message);
}
finally
{
CloseConnection();
}
return dataTable;
}
Here how I call it
string author = "Alfred Schmidt";
int id = 1;
// ExecuteEditQuery("UPDATE Books SET Title =#param1 WHERE idBook =#param2", sqlParameters);
//SqlParameter[] sqlParameters = new SqlParameter[1]
//{
// new SqlParameter ("#param1",author),
//};
SqlParameter[] myparm = new SqlParameter[1];
myparm[0] = new SqlParameter("#Author", SqlDbType.NVarChar, 200);
myparm[0].Value = author;
String query = #"SELECT * FROM Books WHERE Author =#Author";
DataTable dt = ExecuteSelectQuery(query, myparm);
for (int i = 0; i < dt.Rows.Count; i++)
{
Console.WriteLine(dt.Rows.ToString());
}
Console.Write("");
1
Is your OpenConnection() method returns a connection object. It may couse the error, the implementation of the method is not given. Also the adpater is not defined in the code, may be it can be the cause of error too, if it is not initialized.
And i want to say few things about your code:
1) You have and unnecessary command.ExecuteNonQuery(); statement in your ExecuteSelectQuery method.
2) DataAdapter can directly fill DataTable, you dont have to use DataSet.
Here's a proper rewrite of your method.
protected DataTable ExecuteSelectQuery(String query, params SqlParameter[] sqlParameters)
{
using (SqlCommand command = new SqlCommand())
try
{
command.CommandText = query;
command.Parameters.AddRange(sqlParameters);
command.Connection = OpenConnection();
DataTable dataTable = new DataTable();
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
adapter.Fill(dataTable);
return dataTable;
}
catch (SqlException e)
{
return null;
throw new Exception("Error :" + e.Message);
}
finally
{
CloseConnection();
}
}
Note that the SqlDataAdapter can Open() and Close() the connection by itself, if the SqlConnection is Closed when Fill is called.

Would executing the SqlCommand be completely moot/redundant, and slow things down?

Thanks to some tips and reminders here, I changed my code from this kludgy mess:
try
{
DataSet dsUsage = new DataSet();
SqlConnection conn = new SqlConnection("SERVER=PROSQL05;DATABASE=platypusdata;UID=duckbill;PWD=poisonToe42;Connection Timeout=0");
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = String.Format("Exec sp_ViewProductUsage_MappingRS '{0}', '{1}', '{2}'", mammal, dateBegin, dateEnd);
da.SelectCommand = cmd;
conn.Open();
da.Fill(dsUsage);
conn.Close();
DataTable dtUsage = dsUsage.Tables[0];
if (dtUsage.Rows.Count > 0)
{
foreach (DataRow productUsageByMonthDataRow in dtUsage.Rows)
{
. . .
...to this:
try
{
SqlDataAdapter da = new SqlDataAdapter();
DataSet dsUsage = new DataSet();
using (SqlConnection conn = new SqlConnection(UsageRptConstsAndUtils.PlatypusConnStr))
{
using (SqlCommand cmd = new SqlCommand("sp_ViewProductUsage_MappingRS", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#Unit", SqlDbType.VarChar).Value = _unit;
cmd.Parameters.Add("#BegDate", SqlDbType.DateTime).Value = dtBegin;
cmd.Parameters.Add("#EndDate", SqlDbType.DateTime).Value = dtEnd;
da.SelectCommand = cmd;
conn.Open();
//cmd.ExecuteReader(); <- Is this even necessary?
da.Fill(dsUsage);
}
}
DataTable dtUsage = dsUsage.Tables[0];
if (dtUsage.Rows.Count > 0)
{
// Populate the cells
foreach (DataRow productUsageByMonthDataRow in dtUsage.Rows)
{
. . .
Note that I have SqlCommand's ExecuteReader commented out in the new code because it seems unnecessary due to the SqlDataAdapter being provided the SqlCommand. It works fine. So: am I correct in assuming I can remove cmd.ExecuteReader() altogether? Is there any benefit in retaining it, or would that be totally redundant and create "busy work" for the process?
UPDATE
So, to pass an array of SqlParameter (to the ExecuteDataSet method in MethodMan's answer), I take it that I would first have to do something like:
SqlParameter sqlp = new SqlParameter();
sqlp.ParameterName = "Unit";
sqlp.Value = _unit;
cmd.Parameters.Add(sqlp);
...etc. (and then add them to an array - or, possibly better a generic list of SqlParameter).
UPDATE 2
I just ran into this for the first time: if you use MethodMan's example (which I do) and you use a parameterless query, you need to bypass the parameter-adding loop like so:
if (null != parameters)
{
foreach (var item in parameters)
{
cmd.Parameters.Add(item);
}
}
I would personally create a SqlDBHelper class and pass call the stored procedure using a method such as this
public static class SqlDBHelper
{
public static DataSet ExecuteDataSet(string sql, CommandType cmdType, params SqlParameter[] parameters)
{
using (DataSet ds = new DataSet())
using (SqlConnection connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConn"].ConnectionString))
using (SqlCommand cmd = new SqlCommand(sql, connStr))
{
cmd.CommandType = cmdType;
foreach (var item in parameters)
{
cmd.Parameters.Add(item);
}
try
{
cmd.Connection.Open();
new SqlDataAdapter(cmd).Fill(ds);
}
catch (SqlException ex)
{
//log to a file or write to Console for example
Console.WriteLine(ex.Message);
}
return ds;
}
}
}
If you want to return a DataTable then change the return type in the Method signature and call the following in the return statement below
public static DataTable ExecuteDataSet(string sql, CommandType cmdType, params SqlParameter[] parameters)
return ds.Tables[0];
Here is an example on how you would call the method
someDataTable = SqlDBHelper.ExecuteDataSet("sp_ViewProductUsage_MappingRS", CommandType.StoredProcedure,
new SqlParameter() { ParameterName = "#Unit", SqlDbType = SqlDbType.VarChar, Value = _unit },
new SqlParameter() { ParameterName = "#BegDate", SqlDbType = SqlDbType.DateTime, Value = dtBegin },
new SqlParameter() { ParameterName = "#EndDate", SqlDbType = SqlDbType.DateTime, Value = dtEnd }
);

Unable to execute MySql stored procedure from c#

In MySql:
DELIMITER //
DROP PROCEDURE IF EXISTS `testdb`.`Check_UserId_Sproc` //
CREATE PROCEDURE `testdb`.`Check_UserId_Sproc` (IN User_Id NVARCHAR(100))
BEGIN
select count(*) from demo_user where userid = User_Id;
END //
DELIMITER ;
In C#:
public DataTable ExecuteParameterizedSelectCommand(string CommandName, CommandType cmdType,MySqlParameter[] param)
{
DataTable table = new DataTable();
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using(MySqlConnection con = new MySqlConnection(CS))
{
using (MySqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = CommandName;
cmd.Parameters.AddRange(param);
try
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
{
da.Fill(table);
}
}
catch
{
throw;
}
return table;
}
}
}
public DataTable checkExistingUserId()
{
MySqlDBHelper oHelper = new MySqlDBHelper();
MySqlParameter[] parameters = new MySqlParameter[]
{
new MySqlParameter("User_Id", 'DemoId')
};
return oHelper.ExecuteParameterizedSelectCommand("Check_UserId_Sproc", CommandType.StoredProcedure, parameters);
}
When I try to execute the checkExistingUserId(), I get following exception:
Incorrect number of arguments for PROCEDURE testdb.Check_UserId_Sproc; expected 1, got 0
May be I am doing a silly mistake but I am not able to figure it out. I am new to mysql and trying to work around it.
When I debug the array contains the parameter as seen in below image, but it is not collected by the SP.
Thanks in advance
In your code:
cmd.CommandType = CommandType.Text;
should be
cmd.CommandType = cmdType;

Categories

Resources