Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass int parameters in Sql commandText

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.

like image 509
user1627138 Avatar asked Sep 07 '12 06:09

user1627138


People also ask

How do I pass an int array to a stored procedure in C#?

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()).

How do you add parameters in Ado net?

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.


Video Answer


1 Answers

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();
    }
}
like image 56
aiodintsov Avatar answered Sep 21 '22 15:09

aiodintsov