How to pass an integer value like SQL command parameters?
I am trying like this:
cmd.CommandText = (\"insert_questions \'\" +
cmd.Parameters.AddWith
Try this:
cmd.CommandText = ("insert_questions @store_result, @store_title, @store_des");
cmd.Parameters.AddWithValue("@store_result", store_result);
cmd.Parameters.AddWithValue("@store_title", store_title);
cmd.Parameters.AddWithValue("@store_des", store_des);
it should be like this,
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = ("insert_questions") ;
cmd.Parameters.AddWithValue("@value", valueHere);
cmd.Parameters.AddWithValue("@value2", valueHere);
note that @value
and @value2
are the parameters declared in your stored procedure.
You don't concatenate the SqlParameter instances; instead:
cmd.CommandText = "insert_questions @store_result, @store_title, @store_des";
cmd.Parameters.AddWithValue("store_result", store_result);
cmd.Parameters.AddWithValue("store_title", store_title);
cmd.Parameters.AddWithValue("store_des", store_des);
The names used in AddWithValue
are used in the TSQL as @store_result
, etc.
If we assume that inert_questions
is actually a proc, then it is even simpler:
cmd.CommandText = "insert_questions";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("store_result", store_result);
cmd.Parameters.AddWithValue("store_title", store_title);
cmd.Parameters.AddWithValue("store_des", store_des);
Alternatively, if all of that seems tedious, tools like dapper-dot-net make this easier:
someOpenConnection.Execute("insert_questions",
new { store_result, store_title, store_des },
commandType: CommandType.StoredProcedure);
private void ProcessData(MerchantLead data)
{
var cmdCardJournal = new SqlCommand { CommandText = "BusinessLeadsInsert" };
var sql = new Sqlquery();
sql._cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
cmdCardJournal.Parameters.AddWithValue("@Name", data.FirstName);
cmdCardJournal.Parameters.AddWithValue("@LastName", data.LastName);
cmdCardJournal.Parameters.AddWithValue("@BusinessName", data.BusinessName);
cmdCardJournal.Parameters.AddWithValue("@PhoneNumber", data.PhoneNumber);
cmdCardJournal.Parameters.AddWithValue("@Email", data.Email);
cmdCardJournal.Parameters.AddWithValue("@Website", data.Website);
cmdCardJournal.Parameters.AddWithValue("@OTP", data.OTP);
cmdCardJournal.Parameters.AddWithValue("@OTPExpired", DateTime.UtcNow.AddMinutes(30));
sql.SpCommandExeNon(cmdCardJournal);
}
the correct way to go is
using(var connection = new SqlConnection(ConnectionString))
{
connection.Open();
using(var command = new SqlCommand("SELECT * FROM Table WHERE ID=@someID",connection))
{
command.Parameters.AddWithValue("someID",1234);
var r = command.ExecuteQuery();
}
}
this means it works even with text queries. it's even easier with stored procedures - instead of sql query you just provide stored procedure name:
using(var connection = new SqlConnection(ConnectionString))
{
connection.Open();
using(var command = new SqlCommand("insert_sproc",connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("someID",1234);
var r = command.ExecuteQuery();
}
}