More Efficient Way of Adding Parameters to a SqlCommand .NET

后端 未结 3 912
臣服心动
臣服心动 2021-01-06 09:19

I was just reading a question about how to add parameters to a SqlCommand in .NET, and it raised a question for me. In all of my programs, this is how I add parameters to m

相关标签:
3条回答
  • 2021-01-06 09:34

    This is how I am doing it in code:

    cmd.Parameters.AddWithValue("@name", value);
    
    0 讨论(0)
  • 2021-01-06 09:41

    here is my way

    cmd.InjectFrom<SetParamsValues>(foo);
    

    all the properties foo (I sometimes use anonymous, and you can also specify ignores ) are going to be AddWithValue to the cmd, and for null DBNull.Value is going to be set

    I recommend you too look at samples project from here http://valueinjecter.codeplex.com/ (it contains DAL sample)

    0 讨论(0)
  • 2021-01-06 09:42

    Both ways are going to create objects - whether you call the constructor yourself or whether another method does, the object is still going to be created.

    More importantly, however, you're about to make a database call. The cost of creating a dozen objects is going to be absolutely peanuts compared with the database call, even if it's a very fast call.

    Don't worry about it - just use the most readable code.

    0 讨论(0)
提交回复
热议问题