sqlconnection

Do I have to Close() a SQLConnection before it gets disposed?

前提是你 提交于 2019-11-26 12:40:54
问题 Per my other question here about Disposable objects, should we call Close() before the end of a using block? using (SqlConnection connection = new SqlConnection()) using (SqlCommand command = new SqlCommand()) { command.CommandText = \"INSERT INTO YourMom (Amount) VALUES (1)\"; command.CommandType = System.Data.CommandType.Text; connection.Open(); command.ExecuteNonQuery(); // Is this call necessary? connection.Close(); } 回答1: Since you have a using block, the Dispose method of the SQLCommand

Under what circumstances is an SqlConnection automatically enlisted in an ambient TransactionScope Transaction?

僤鯓⒐⒋嵵緔 提交于 2019-11-26 12:33:54
What does it mean for an SqlConnection to be "enlisted" in a transaction? Does it simply mean that commands I execute on the connection will participate in the transaction? If so, under what circumstances is an SqlConnection automatically enlisted in an ambient TransactionScope Transaction? See questions in code comments. My guess to each question's answer follows each question in parenthesis. Scenario 1: Opening connections INSIDE a transaction scope using (TransactionScope scope = new TransactionScope()) using (SqlConnection conn = ConnectToDB()) { // Q1: Is connection automatically enlisted

Why doesn't Dapper dot net open and close the connection itself?

别说谁变了你拦得住时间么 提交于 2019-11-26 11:21:03
问题 Dapper implicitly expects a connection to be open when it uses it. Why doesn\'t it open and close it itself? Wouldn\'t this simply connection management? I ask because a co-worker and I have been going back and forth on the nature of what goes on behind the scenes with connection pooling, and if there is any benefit to keeping a connection open amongst multiple commands, or to open and close it for each command. 回答1: Dapper now (and for quite some time) deals with this internally. It just

How to run multiple SQL commands in a single SQL connection?

纵然是瞬间 提交于 2019-11-26 09:28:02
问题 I am creating a project in which I need to run 2-3 sql commands in a single sql connection. Here is the code I have written: SqlConnection con = new SqlConnection(@\"Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\project.mdf;Integrated Security=True\"); con.Open(); SqlCommand cmd = new SqlCommand(\"select * from \" + mytags.Text + \" \", con); SqlDataReader rd = cmd.ExecuteReader(); if (rd.Read()) { con.Close(); con.Open(); SqlCommand cmd1 = new SqlCommand(\"insert into \" +

What is the difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout?

巧了我就是萌 提交于 2019-11-26 08:09:11
问题 Is there any difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout in .NET? 回答1: Yes. CommandTimeout is how long a single command can take to complete. ConnectionTimeout is how long it can take to establish a connection to the server to start with. For instance, you may be executing relatively long-running queries - it's perfectly okay for them to take 10 minutes to complete, but if it took 10 minutes to make the connection to start with, you'd know that something

ASP.NET use SqlConnection connect MySQL

自作多情 提交于 2019-11-26 07:45:30
问题 This is the connection string saved in web.config : <appSettings> <add key=\"conn\" value=\"Driver={MySQL ODBC 5.1 Driver};server=127.0.0.1;uid=root;pwd=1234;database=gis_server;option=3\"/> </appSettings> This is the code to connect to the database: protected bool CheckPasswordBySqlServer(string strEmail, string strPsw) { if (strEmail.ToLower() == \"admin\") { return false; } string str = \"select id,Rank,RankEnc,ParentUser,Company from tbl_User where userName=@UserName and password1=

Is SqlCommand.Dispose() required if associated SqlConnection will be disposed?

妖精的绣舞 提交于 2019-11-26 07:43:20
问题 I usually use code like this: using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings[\"MyConn\"].ConnectionString)) { var command = connection.CreateCommand(); command.CommandText = \"...\"; connection.Open(); command.ExecuteNonQuery(); } Will my command automatically disposed? Or not and I have to wrap it into using block? Is it required to dispose SqlCommand ? 回答1: Just do this: using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn

Does SqlCommand.Dispose close the connection?

天大地大妈咪最大 提交于 2019-11-26 07:28:57
问题 Can I use this approach efficiently? using(SqlCommand cmd = new SqlCommand(\"GetSomething\", new SqlConnection(Config.ConnectionString)) { cmd.Connection.Open(); // set up parameters and CommandType to StoredProcedure etc. etc. cmd.ExecuteNonQuery(); } My concern is : Will the Dispose method of the SqlCommand (which is called when exiting the using block) close the underlying SqlConnection object or not? 回答1: No, Disposing of the SqlCommand will not effect the Connection. A better approach

Under what circumstances is an SqlConnection automatically enlisted in an ambient TransactionScope Transaction?

删除回忆录丶 提交于 2019-11-26 02:59:32
问题 What does it mean for an SqlConnection to be \"enlisted\" in a transaction? Does it simply mean that commands I execute on the connection will participate in the transaction? If so, under what circumstances is an SqlConnection automatically enlisted in an ambient TransactionScope Transaction? See questions in code comments. My guess to each question\'s answer follows each question in parenthesis. Scenario 1: Opening connections INSIDE a transaction scope using (TransactionScope scope = new

“open/close” SqlConnection or keep open?

扶醉桌前 提交于 2019-11-26 02:51:42
问题 I have my business-logic implemented in simple static classes with static methods. Each of these methods opens/closes SQL connection when called: public static void DoSomething(string something) { using (SqlConnection connection = new SqlConnection(\"...\")) { connection.Open(); // ... connection.Close(); } } But I think avoiding opening and closing a connection saves performance . I made some tests loooong time ago with OleDbConnection class (not sure about SqlConnection), and it definitely