Tag: sqlconnection

如何使用ConfigurationManager.AppSettings

我从来没有使用过“appSettings”之前。 如何在C#中configuration这个与SqlConnection一起使用,这是我使用的“ConnectionStrings” SqlConnection con = new SqlConnection(); con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 这就是我对“appSettings” SqlConnection con = new SqlConnection(); con = ConfigurationManager.AppSettings("ConnectionString"); 但它不工作。

什么时候应该打开和closures连接到SQL Server

我有一个简单的静态类,其中有几个方法。 每个方法打开一个SqlConnection,查询数据库并closures连接。 这样,我确信我总是closures与数据库的连接,但另一方面,我不喜欢总是打开和closures连接。 下面是我的方法的例子。 public static void AddSomething(string something) { using (SqlConnection connection = new SqlConnection("…")) { connection.Open(); // … connection.Close(); } } 考虑到方法是在一个静态类,我应该有一个静态成员包含单个SqlConnection? 我应该如何以及何时放弃它? 什么是最佳实践?

用一个连接执行多个sql命令更好吗,还是每次都重新连接?

这里是我的testing代码,这似乎表明最好连接多次而不是连接一次。 难道我做错了什么? 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; […]

更改SqlConnection超时

我想重写15秒的默认SqlConnection超时,并得到一个错误说 属性或索引器不能被分配,因为它是只读的。 有没有解决的办法? 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(); } }

SQL Server在Windows应用程序中返回错误“用户login失败NT AUTHORITY \ ANONYMOUS LOGON”

最近开始一直没有问题的应用程序(在大约6个月左右没有进行任何积极的开发)开始无法连接到数据库。 操作pipe理员不能说什么可能会导致问题的变化。 客户端应用程序使用集成安全性= True的硬编码连接string,但是当应用程序试图创build到数据库的连接时,它会抛出一个SQLException,指出“用户login失败”NT AUTHORITY \ ANONYMOUS LOGON。 我可以通过此帐户上的Management Studiologin到数据库没有问题。 我在这个问题上看到的所有东西都是针对ASP.NET项目的,显然这是一个“双跳问题”,作为一个客户端应用程序,最好不要成为问题。 任何帮助将不胜感激。 编辑 客户机和服务器机器以及用户帐户在同一个域中。 这是在Windows防火墙closures时发生的。 主要理论是:服务器在大约一周前重启,未能注册服务主体名称(Service Principal Name,SPN)。 未能注册SPN可能会导致集成身份validation返回到NTLM而不是Kerberos。

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

.NET中的SqlCommand.CommandTimeout和SqlConnection.ConnectionTimeout是否有区别?

检查SQL连接是打开还是closures

你如何检查是否打开或closures我正在使用 if (SQLOperator.SQLCONNECTION.State.Equals("Open")) 然而,即使国家是“公开”,它在这个检查失败。

我必须在处理之前closures()一个SQLConnection吗?

在这里关于一次性对象的其他问题 ,我们应该在使用块结束之前调用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(); }

如果关联的SqlConnection将被丢弃,是否需要SqlCommand.Dispose()?

我通常使用这样的代码: using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString)) { var command = connection.CreateCommand(); command.CommandText = "…"; connection.Open(); command.ExecuteNonQuery(); } 我的command自动处理吗? 或者不是,我必须把它包装进using块? 是否需要处理SqlCommand ?

SqlCommand.Dispose是否closures连接?

我可以有效地使用这种方法吗? using(SqlCommand cmd = new SqlCommand("GetSomething", new SqlConnection(Config.ConnectionString)) { cmd.Connection.Open(); // set up parameters and CommandType to StoredProcedure etc. etc. cmd.ExecuteNonQuery(); } 我关心的是:SqlCommand的Dispose方法(在退出using块时调用)是否会closures底层的SqlConnection对象?