Tag: database connection

关闭SQLDataReaders – 如何判断它们是否已关闭?

我发现我有一些网站连接池问题,我正在跟踪它们。 我知道有一件事要确保任何SQLDataReaders都关闭,我已经走了,并确保它们是。 我脑子里浮现的一个问题是关于返回SQLDataReaders的方法以及它们如何关闭(或不关闭)。 所以这里有我设置的东西和一些示例方法: public static SqlDataReader ExecuteReader(SqlCommand cmd) { SqlConnection c = new SqlConnection(Properties.Settings.Default.DatabaseConn); cmd.Connection = c; c.Open(); return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); } 然后我有一个使用’ExecuteReader()’的方法 public static SqlDataReader GetData() { SqlCommand Query = new SqlCommand(“select * from SomeTable”); return ExecuteReader(Query); } 现在说我有另一个调用’GetData’的方法。 我显然简化了一些事情 public static SqlDataReader GetMoreData() { return GetData; } 所以我的问题是,当我像这样调用’GetMoreData’时 SqlDataReader dr = GetMoreData(); //do […]

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 […]

SqlCeConnection以什么格式期望其连接字符串arg?

我有一个失败的SqlCeConnection.Open()调用。 我得到的exception消息非常通用:“System.Data.SqlserverCe.SqlCeException”(比漂亮的更通用) 相关代码是: private DBConnection() { try { string conStr = “Data Source = ” + filename; string cmpStr = conStr + “.tmp”; if (File.Exists(filename+”.tmp”)) File.Delete(filename+”.tmp”); engine = new SqlCeEngine(conStr); if (File.Exists(filename)) { } else { engine.CreateDatabase(); } engine.Dispose(); objCon = new SqlCeConnection(conStr); //MessageBox.Show(string.Format(“conStr == {0}”, conStr)); objCon.Open(); // <= This is where all Dallas […]

使用脚本启用到sql express的远程连接

我正在使用sql server express 2008部署应用程序。在我的应用程序的先决条件部分中,我包括: 因此,当用户安装我的应用程序时,它也将安装sql express。 然后我将能够连接到该数据库引擎: try { // database should be in the same network SqlConnection conn = new SqlConnection(@”Data Source=.\sqlexpress; Integrated Security=True”); conn.Open(); MessageBox.Show(“Connection succesfull”); } catch { MessageBox.Show(“Unable to connect”); } 现在,当我安装不同的应用程序(客户端版本)时,我希望能够连接到该数据库引擎。 我设法通过以下方式连接到它: try { SqlConnection conn = new SqlConnection(@”Data Source=192.168.0.120\sqlexpress,22559; USER=sa; PASSWORD=*********”); conn.Open(); MessageBox.Show(“Connection succesfull”); } catch { MessageBox.Show(“Unable to […]

我是否需要显式处置SqlDataAdapter?

在这个线程中 ,有一个建议是在操作之后, SqlDataAdapter的实例被明确地处理掉。 String connString = @”your connection string here”; String query = “select * from table”; SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(query, conn); conn.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dataTable); conn.Close(); da.Dispose(); 真的有必要吗? GC怎么样?