使用“使用(conn)”时连接的接近程度,因为它在另一个方法中导致错误,即连接未初始化。

时间:2015-02-23 14:54:52

标签: sql asp.net initialization using

我有一个页面,它使用连接字符串显示来自数据库的信息,一个是SqlDataReader,其中我.open和.close连接。另一个是

Using(conn)
{
}

使用在打开/关闭功能之前运行,当发生这种情况时,会导致连接未正确初始化的错误。当没有调用Using()函数时,一切都很好。如果我再次在SqlDataReader函数中设置连接字符串它可以正常工作,但是其他方面以前设置的字符串只是空的。

我是asp.net的新手,我不确定在页面上使用Using()是不好的做法。

我在页面加载时设置了连接字符串,但如果运行了Using(),则连接字符串s始终为空:

    public SqlConnection conn;
    String albumID;

    protected void Page_Load(object sender, EventArgs e)
    {
        conn= new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\connstring.mdf;Integrated Security=True");
        thing1();
        thing_using_Using();
        conn_open_conn_close_thing();
        thing2();
    }

Using()函数:

    public void isAlreadyLiked()
    {
        DataTable dt = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Likes WHERE UserID = @userID AND AlbumID = @albumID", conn);
        using (conn)
        {
            sda.SelectCommand.Parameters.AddWithValue("@userID", Session["loggedID"]);
            sda.SelectCommand.Parameters.AddWithValue("@albumID", albumID);

            sda.Fill(dt);

            if (dt.Rows.Count == 0)
            {
                //place Like button here
            }
            else
            {
                //place Unlike button here
            }
        }
    }

使用.open和.close的函数:

    public void albumDetails()
    {        
    //if the conn string is set here again its fine, but I don't want to repeat the conn string over and over.    

        SqlCommand comm = new SqlCommand("SELECT * FROM Albums WHERE AlbumID=" + albumID, conn);
        conn.Open();
        SqlDataReader reader = comm.ExecuteReader();
        while (reader.Read())
        {
            string AlbumName = reader["AlbumName"].ToString();
            string AlbumArtist = reader["AlbumArtist"].ToString();
            lblAlbumName.Text += AlbumName;
            lblAlbumArtist.Text += AlbumArtist;
        }
        reader.Close();
        conn.Close();
    }

1 个答案:

答案 0 :(得分:1)

将connectionstring保持为全局变量或者更好地从web.config读取它并在每次需要时重新创建SqlConnection,在使用后立即销毁它。

对于像连接这样的一次性物品,这是推荐的做法,毕竟用Connection Pooling你不必为演出担心

// Here is as fixed text and this could be probematic if 
// you need to deploy to another environment. 
// Better to read it from your web.config
public string conString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\connstring.mdf;Integrated Security=True"
...

protected void Page_Load(object sender, EventArgs e)
{
  ....
}

public void isAlreadyLiked()
{
    DataTable dt = new DataTable();
    using(SqlConnection con = new SqlConnection(conString))
    using(SqlDataAdapter sda = new SqlDataAdapter(".....", conn);
    {
        ......
    }
}

public void albumDetails()
{        
    using(SqlConnection con = new SqlConnection(conString))
    using(SqlCommand comm = new SqlCommand(".....", conn);
    {
        conn.Open();
        using(SqlDataReader reader = comm.ExecuteReader())
        {
            while (reader.Read())
            {
                ...
            }
        }
    }
}