How to pass int parameters in Sql commandText

前端 未结 5 1520
庸人自扰
庸人自扰 2021-01-12 01:40

How to pass an integer value like SQL command parameters?

I am trying like this:

cmd.CommandText = (\"insert_questions \'\" + 
cmd.Parameters.AddWith         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-12 02:08

    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);
    

提交回复
热议问题