Questions tagged «sqlconnection»

3
在什么情况下SqlConnection会自动加入环境TransactionScope事务中?
SqlConnection在事务中被“征募”是什么意思?这是否仅表示我在连接上执行的命令将参与事务? 如果是这样,在什么情况下SqlConnection会自动加入环境TransactionScope事务中? 查看代码注释中的问题。我对每个问题答案的猜测都跟在括号中的每个问题之后。 方案1:在事务范围内打开连接 using (TransactionScope scope = new TransactionScope()) using (SqlConnection conn = ConnectToDB()) { // Q1: Is connection automatically enlisted in transaction? (Yes?) // // Q2: If I open (and run commands on) a second connection now, // with an identical connection string, // what, if any, is …

7
在“使用”块中,是否在返回或异常时关闭SqlConnection?
第一个问题: 说我有 using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string storedProc = "GetData"; SqlCommand command = new SqlCommand(storedProc, connection); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@EmployeeID", employeeID)); return (byte[])command.ExecuteScalar(); } 连接是否关闭?因为从技术上讲,我们永远都无法}像return以前那样走到最后。 第二个问题: 这次我有: try { using (SqlConnection connection = new SqlConnection(connectionString)) { int employeeID = findEmployeeID(); connection.Open(); SqlCommand command = new …
136 c#  using  sqlconnection 

6
“打开/关闭” SqlConnection还是保持打开状态?
我用静态方法在简单的静态类中实现了我的业务逻辑。这些方法中的每一个在调用时都会打开/关闭SQL连接: public static void DoSomething(string something) { using (SqlConnection connection = new SqlConnection("...")) { connection.Open(); // ... connection.Close(); } } 但是我认为避免打开和关闭连接会节省性能。我很久以前用OleDbConnection类(不确定SqlConnection)进行了一些测试,它确实可以像这样工作(据我所记得): //pass the connection object into the method public static void DoSomething(string something, SqlConnection connection) { bool openConn = (connection.State == ConnectionState.Open); if (!openConn) { connection.Open(); } // .... if …
121 c#  sqlconnection 

8
我是否必须先关闭SQLConnection的SQLConnection?
根据我在此处有关Disposable对象的其他问题,是否应该在using块结束之前调用Close()? 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(); }



9
更改SqlConnection超时
我试图覆盖默认SqlConnection的15秒超时,并收到一条错误消息,指出 无法分配属性或索引器,因为它是只读的。 有没有解决的办法? using (SqlConnection connection = new SqlConnection(Database.EstimatorConnection)) { connection.Open(); using (SqlCommand command = connection.CreateCommand()) { command.CommandType = CommandType.StoredProcedure; connection.ConnectionTimeout = 180; // This is not working command.CommandText = "sproc_StoreData"; command.Parameters.AddWithValue("@TaskPlanID", order.Projects[0].TaskPlanID); command.Parameters.AddWithValue("@AsOfDate", order.IncurDate); command.ExecuteNonQuery(); } }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.