标签: sqlconnection

在什么情况下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 the relationship of this second connection to the first?
    //
    // Q3: Will this second connection's automatic enlistment
    // in the current transaction scope cause the transaction to …
Run Code Online (Sandbox Code Playgroud)

c# ado.net sqlcommand sqlconnection transactionscope

198
推荐指数
2
解决办法
3万
查看次数

在"使用"块中是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();
}
Run Code Online (Sandbox Code Playgroud)

连接是否关闭?因为从技术上讲,我们永远不会}return以前那样到达最后.

第二个问题:
这次我有:

try
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        int employeeID = findEmployeeID();

        connection.Open();
        SqlCommand command = new SqlCommand("UpdateEmployeeTable", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter("@EmployeeID", employeeID));
        command.CommandTimeout = 5;

        command.ExecuteNonQuery();
    }
}
catch (Exception) { /*Handle error*/ }
Run Code Online (Sandbox Code Playgroud)

现在,说出来try我们得到一个错误,它被抓住了.连接是否仍然关闭?因为我们再次跳过其余代码try并直接转到catch …

c# using sqlconnection

134
推荐指数
5
解决办法
16万
查看次数

在处理之前我是否必须关闭()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();
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net using sqlcommand sqlconnection

111
推荐指数
4
解决办法
4万
查看次数

"打开/关闭"SqlConnection还是保持打开状态?

我的业务逻辑是用静态方法在简单的静态类中实现的.这些方法中的每一个在调用时打开/关闭SQL连接:

public static void DoSomething(string something)
{
    using (SqlConnection connection = new SqlConnection("..."))
    {
        connection.Open();

        // ...

        connection.Close();
    }
}
Run Code Online (Sandbox Code Playgroud)

但我认为避免打开和关闭连接可以节省性能.我以前用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 (openConn) 
    {
        connection.Close();
    }
}
Run Code Online (Sandbox Code Playgroud)

所以问题是 - 我应该选择方法(a)还是方法(b)?我读了另一个stackoverflow问题,连接池为我保存了性能,我根本不用费心......

PS.它是一个ASP.NET应用程序 - 仅在Web请求期间存在连接.不是win-app或服务.

c# sqlconnection

103
推荐指数
5
解决办法
8万
查看次数

检查SQL连接是打开还是关闭

你如何检查我使用的是打开还是关闭

 if (SQLOperator.SQLCONNECTION.State.Equals("Open"))
Run Code Online (Sandbox Code Playgroud)

然而,即使是国家"开放",它也没有通过这项检查.

c# ado.net sqlconnection

99
推荐指数
6
解决办法
26万
查看次数

SqlCommand.CommandTimeout和SqlConnection.ConnectionTimeout有什么区别?

.NET SqlCommand.CommandTimeoutSqlConnection.ConnectionTimeout.NET 之间有什么区别吗?

.net sqlcommand sqlconnection

81
推荐指数
4
解决办法
6万
查看次数

更改SqlConnection超时

我试图覆盖SqlConnection15秒的默认超时,并得到一个错误说

无法分配属性或索引器,因为它是只读的.

有没有解决的办法?

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# sql-server sqlconnection

72
推荐指数
8
解决办法
13万
查看次数

SQL Server返回错误"用户登录失败'NT AUTHORITY\ANONYMOUS LOGON'." 在Windows应用程序中

最近开始无法连接到数据库的应用程序一直没有问题(并且在大约6个月左右没有对其进行任何活动开发).操作管理员无法说出可能导致问题的更改内容.

客户端应用程序使用具有Integrated Security = True的硬编码连接字符串,但是当应用程序尝试创建与数据库的连接时,它会抛出一个SQLException,表示"用户登录失败'NT AUTHORITY\ANONYMOUS LOGON".

我可以通过此帐户上的Management Studio登录数据库而不会出现问题.我在这个问题上看到的所有内容都是针对ASP.NET项目的,显然它是"双跳问题",它是一个客户端应用程序,最好不要成为问题.任何帮助将不胜感激.

编辑

客户端计算机和服务器计算机以及用户帐户位于同一域中.Windows防火墙关闭时会发生这种情况.

主要理论是:服务器大约一周左右重新启动,并且未能注册服务主体名称(SPN).未注册SPN可能导致集成身份验证回退到NTLM而不是Kerberos.

sql-server security sqlconnection

62
推荐指数
4
解决办法
20万
查看次数

SqlCommand.Dispose是否关闭连接?

我可以有效地使用这种方法吗?

using(SqlCommand cmd = new SqlCommand("GetSomething", new SqlConnection(Config.ConnectionString))
{
    cmd.Connection.Open();
    // set up parameters and CommandType to StoredProcedure etc. etc.
    cmd.ExecuteNonQuery();
}
Run Code Online (Sandbox Code Playgroud)

我担心的是:SqlCommand的Dispose方法(在退出using块时调用)是否会关闭底层的SqlConnection对象?

.net garbage-collection dispose sqlcommand sqlconnection

51
推荐指数
2
解决办法
4万
查看次数

使用一个连接执行多个sql命令或每次重新连接是否更好?

这是我的测试代码,它似乎表明连接多次而不是仅连接一次更好.

难道我做错了什么?

int numIts = 100;
Stopwatch sw = new Stopwatch();
sw.Start();
using (SqlConnection connection = new SqlConnection(connectionParameters))
{   
            connection.Open();
    for(int i = 0; i < numIts; i++)
    {
        SqlCommand command = new SqlCommand(sqlCommandName, connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue(par1Name, par1Val);
                command.Parameters.AddWithValue(par2Name, par2Val);
        using(SqlDataReader reader = command.ExecuteReader())
        {
        }
    }
}
sw.Stop();
TimeSpan durationOfOneConnectionManyCommands = sw.Elapsed;
Console.WriteLine(durationOfOneConnectionManyCommands);

sw.Reset();

sw.Start();
for(int i = 0; i < numIts; i++)
{
    using (SqlConnection connection = new SqlConnection(connectionParameters))
    {   
                connection.Open();
        SqlCommand command = new SqlCommand(sqlCommandName, …
Run Code Online (Sandbox Code Playgroud)

.net c# sql-server performance sqlconnection

42
推荐指数
4
解决办法
4万
查看次数