How to pass an integer value like SQL command parameters?
I am trying like this:
cmd.CommandText = ("insert_questions '" +
cmd.Parameters.AddWithValue(store_result,store_result) + "','" +
cmd.Parameters.AddWithValue(store_title, store_title) + "', '" +
cmd.Parameters.AddWithValue(store_des, store_des) + "'");
store_result is int and other 2 parameter are string type.
store_result is giving a error message like below.
Argument 1: cannot convert from 'int' to 'string'
in SP ,there is a another int type variable which will get store_result's value.
What is correct syntax for passing int parameters?
Thank you.
You can convert your array to string in C# and pass it as a Stored Procedure parameter as below, int[] intarray = { 1, 2, 3, 4, 5 }; string[] result = intarray. Select(x=>x. ToString()).
Should use something like the following: SqlCommand cmd = new SqlCommand("INSERT INTO Product_table Values(@Product_Name, @Product_Price, @Product_Profit, @p)", connect); cmd. Parameters. Add("@Product_Name", SqlDbType.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With