Tag: sqlconnection

SqlConnection线程安全吗?

我有一个Log类,它将日志放在Windows日志和SQL表中。 为了优化我的代码,我想只使用一个SqlConnection 。 在MSDN中,它说:此类型的任何public static (在Visual Basic中为Shared)成员都是线程安全的。 任何实例成员都不保证是线程安全的。 我的问题是: private static readonly SqlConnection conn = new SqlConnection(ConfigParameters.Instance.UIDConnection); 它是线程安全的吗? 如果是,使用Open()和Close() ? 如果没有,如何正确使用SqlConnection ? 这是我的完整类代码: private static readonly SqlConnection conn = new SqlConnection(ConfigParameters.Instance.UIDConnection); public static long WriteLog(string sSource, string sMessage, int iErrorCode, EventLogEntryType xErrorType) { // Windows Logs if (ConfigParameters.Instance.WindowsLog) EventLog.WriteEntry(sSource, sMessage, xErrorType, iErrorCode); // SQL Logs […]

Connection.open无限期挂起,不会抛出任何exception

当我尝试执行以下代码时,程序将无限期挂起。 我不知道为什么,似乎还有其他未解答的话题。 虽然,如果无法访问IP \网站,那么它按预期工作。 private void DoStuff() { string connectionString = “Data Source=www.google.com;Connection Timeout=5”; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); //Hangs here indefinitely Console.WriteLine(“Test”); } } 例如,如果我将连接字符串设置为 connectionString = “Data Source=www.nonexistentsite.com;Connection Timeout=5”; 然后它会抛出exception。 如何让它为活动站点抛出exception? …显然, 谷歌只是出于测试目的。 编辑: 如果我尝试连接到无法访问的服务器名称或IP地址,我将收到此exception… A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server […]

调用new SqlConnection()挂起程序

这个让我难过。 我甚至没有尝试连接数据库。 当这段代码到达我实例化一个新的SqlConnection对象的行时,它就会挂起,而不是抛出exception或任何东西。 我已经尝试过为2.0编译它。 3.5和4.0,它们都挂了。 当然它也适用于我的机器和你的机器。 但我试图在Windows Server 2008 x64服​​务器上运行此代码,它不会让步。 // example.cs using System; using System.Data; using System.Data.SqlClient; public class MainClass { public static void Main(string[] args) { Console.WriteLine(“start”); SqlConnection conn = new SqlConnection(); // hangs here Console.WriteLine(“finish”); // never makes it here. } } 编译(2.0):c:\ Windows \ Microsoft.NET \ Framework \ v2.0.50727 \ csc.exe […]

管理SQL Server连接

SQL连接的最佳实践是什么? 目前我使用以下内容: using (SqlConnection sqlConn = new SqlConnection(CONNECTIONSTRING)) { sqlConn.Open(); // DB CODE GOES HERE } 我已经读过这是一种非常有效的SQL连接方式。 默认情况下,SQL池是活动的,因此我理解的是,当using代码结束时, SqlConnection对象被关闭并处理,但是与DB的实际连接被放入SQL连接池中。 我错了吗?

如果您已经关闭了SqlConnection,是否需要关闭/处理SqlDataReader?

我注意到了这个问题 ,但我的问题更加具体。 使用有什么好处 using (SqlConnection conn = new SqlConnection(conStr)) { using (SqlCommand command = new SqlCommand()) { // dostuff } } 代替 using (SqlConnection conn = new SqlConnection(conStr)) { SqlCommand command = new SqlCommand(); // dostuff } 显然,如果您计划使用相同的连接运行多个命令,这很重要,因为关闭SqlDataReader比关闭并重新打开连接更有效(调用conn.Close();conn.Open();也将释放连接)。 我看到许多人坚持认为无法关闭SqlDataReader意味着保留开放的连接资源,但这不仅适用于您不关闭连接吗?

最有效的方法来测试SQL连接字符串的可用性

我有这个代码,我试图让它测试SQL字符串连接,但我不知道如何处理connection.Open = true部分connection.Open = true你能帮我解决这个问题吗? 非常感谢您的参与。 private void button1_Click(object sender, EventArgs e) { try { using (SqlConnection connection = new SqlConnection(“Data Source='” + textBox1.Text + “‘;Initial Catalog='” + textBox2.Text + “‘;User ID='” + textBox3.Text + “‘;Password='” + textBox4.Text + “‘”)) { try { connection.Open(); if (connection.Open == true) // if connection.Open was successful { MessageBox.Show(“You […]

C#DbConnection强制转换为SqlConnection

我在一个应用程序中找到了这段代码 Database database = DatabaseFactory.CreateDatabase(“connection string”); DbConnection connection = database.CreateConnection(); connection.Open(); SqlConnection sqlConnection = (SqlConnection)connection; 是否安全,SqlConnection来自DbConnection。 数据库来自Microsoft.Practices.EnterpriseLibrary.Data。 根据文档,CreteDatabase返回DbConnection。

如何通过SqlConnection获取上次执行的SQL查询?

实际上,我的情景与此处提到的有点不同。 我问了其他问题。 但由于我没有在那里得到解决方案,我决定改变方法。 我的代码可以访问SqlConnection对象。 我无法访问所有其他ADO.NET对象,如SqlCommand , SqlParameter等。 这些其他对象由Dapper Extensions ORM使用。 我的应用程序使用SqlConnection对象和Dapper Extensions方法执行SQL查询。 SQL查询由Dapper Extensions自动生成; 我无法访问生成的查询。 我想记录这个SQL查询。 我已经有了我的日志记录模块,我唯一需要的是连接对象执行的最后一个SQL查询。 如何通过SqlConnection获取上次执行的SQL查询? 以下操作无效,因为无法访问SqlCommand 。 如果我得到底层的SqlCommand ,我可以使用下面的代码从它构建查询; 不幸的是,我无法访问它。 public string GetCommandLogString(IDbCommand command) { string outputText; if(command.Parameters.Count == 0) { outputText = command.CommandText; } else { StringBuilder output = new StringBuilder(); output.Append(command.CommandText); output.Append(“; “); IDataParameter objIDataParameter; int parameterCount = command.Parameters.Count; for(int […]

用户’sa’登录失败。 在连接字符串中

我收到以下错误: 用户’sa’登录失败 当我尝试通过字符串变量设置值来连接服务器时: private SqlConnection getDbConnection = new SqlConnection(“Data Source=”+dbname+”;Initial Catalog=”+catname+”;User Id=sa;Password=sa;Integrated Security=false”); 但是当我使用没有字符串变量的普通连接字符串时,它运行良好: private SqlConnection getDbConnection = new SqlConnection(“Data Source=Ali-pc\\;Initial Catalog=master;User Id=sa;Password=sa;Integrated Security=false”);

“使用SQLConnection对用户”C#登录失败

我一直在尝试通过我的C#代码连接到我的数据库(与我的代码位于同一台计算机上)。 问题是我一直得到“登录失败的用户”“”错误……我承认我对连接数据库的知识很少,我已经尝试了其他问题中的几乎所有步骤! 这是我的代码的一部分: SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[“SQLServerConnection”].ConnectionString); SqlCommand command = connection.CreateCommand(); command.CommandText = @”IF EXISTS ( SELECT * FROM user WHERE EMAILADRES = @email and WACHTWOORD = @password ) SELECT CAST (1 as bit) ELSE SELECT CAST(0 as bit)”; command.Parameters.AddWithValue(“email”, email); command.Parameters.AddWithValue(“password”, password); connection.Open(); object ReturnBool = command.ExecuteScalar(); connection.Close(); 这是我的连接字符串: