Is it better to execute many sql commands with one connection, or reconnect every time?

前端 未结 5 713
醉酒成梦
醉酒成梦 2020-12-01 04:11

Here\'s my test code, which seems to suggest that it\'s better to connect multiple times instead of connecting just once.

Am I doing something wrong?



        
5条回答
  •  醉酒成梦
    2020-12-01 04:49

    By default, SqlConnection will use connection pooling. Therefore your code does most likely not actually open many connections in either case.

    You can control if SqlConnection will use pooling by enabling or disabling the pool in the connectionstring, depending on what DB your connection string is for, the syntax will vary.

    See here for some info if you use MSSQLServer. Try setting Pooling=false in the connection string and see if it makes a difference.

提交回复
热议问题