连接关闭

时间:2016-09-06 12:19:50

标签: c# .net sql-server

我对.net中的sql会话有疑问。

为什么我在拥有相同的id会话和pid后关闭sql连接?

我试过这个测试:

        public void c() 
        {
            SqlConnection conn = null;
            SqlCommand comm = null;
            SqlDataReader data = null;
            String q;

            try
            {
                for (int i = 0; i < 3; i++)
                {
                    conn = new SqlConnection("Server=server;Database=db;User id=user;Password=pass;timeout=10");
                    conn.Open();

                    q =
                    "select connection_id,s.session_id " +
                    "from sys.dm_exec_connections c " +
                    "join sys.dm_exec_sessions s on s.session_id = c.session_ID " +
                    "where s.session_id=@@spid";
                    comm = new SqlCommand(q, conn);
                    data = comm.ExecuteReader();
                    while (data.Read())
                       Console.WriteLine(data[0] + "-" + data[1]);

                    comm.Dispose();
                    data.Dispose();
                    conn.Dispose();
                    conn.Close();
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                if (comm != null)
                    comm.Dispose();
                if (data != null)
                    data.Dispose();
                if (conn != null)
                    conn.Close();
            }
        }

每次会话时我必须做什么才能关闭?

提前谢谢!

!!解决 !! 我插入连接字符串“Pooling = true;”,然后是:

conn = new SqlConnection("Pooling=true;Server=server;Database=db;User id=user;Password=pass;timeout=10");

0 个答案:

没有答案