当前位置:编程学习 > C#/ASP.NET >>

C#如何多次连接数据库啊?

C#如何多次连接数据库啊?以下是小弟的部分代码:
 protected void Page_Load(object sender, EventArgs e)
    {        sc = new SqlConnection("Data Source=(local);Initial Catalog=showyu;Integrated Security=True");
    }
protected void Button4_Click(object sender, EventArgs e)
    {
        string str = "";
        if (TextBox1.Text == "")
        {
            str = "select * from logo";
        }
        else
        {
            str = "select * from logo where id like '%" + TextBox1.Text + "%'";
        }
        SqlDataAdapter sda = new SqlDataAdapter(str, sc);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
try
            {
                sc.Open();
                SqlCommand cmd = new SqlCommand("delete * from logo where id=@id", sc);
                cmd.Parameters.Add("@id", SqlDbType.Text).Value = TextBox2.Text.Trim();
                int k = cmd.ExecuteNonQuery();
                if (k > 0)
                {
                    sc.Close();
                    Response.Write("<script>alert('删除成功!')</script>");
                    Response.Write("<script language='javascript'>document.location = 'sy2.aspx?id=" + id + "&&Access_level=" + Access_level + "'</script>");
                }
                else
                {
                    Response.Write("<script>alert('修改失败!')</script>");
                }
            }
            catch (Exception ee)
            {
                Response.Write("<script>alert('" + ee.Message + "')</script>");
            }
            finally
            {
                sc.Close();
            }
}
为什么我点了第一个按钮后再点第二个按钮就会弹出NaN这个诡异的错误啊?如果我只点了一个就是正常,求解答! --------------------编程问答-------------------- 提示哪一行出错了?
具体的错误信息是什么? --------------------编程问答-------------------- 最好不要在Page_Load中创建连接,需要用的时候,也就是button的响应的时候再建立连接,现在连接都有连接池,需要时再创建new Connection不会有问题,
--------------------编程问答-------------------- 连接数据库 注意 连接开启 和关闭一定要对应 --------------------编程问答-------------------- 请贴出所有错误信息。 --------------------编程问答-------------------- 哪里错了 --------------------编程问答--------------------
引用 3 楼  的回复:
连接数据库 注意 连接开启 和关闭一定要对应
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,