再次求助!“连接未关闭。连接的当前状态为已打开。”
下边是全部代码,我实在整不了了
[code=C#][/code]using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sms_conn;
string sms_sqlstr;
protected void Page_Load(object sender, EventArgs e)
{
string sms_connstr = "Data Source=20110721-1509\\sqlexpress;Initial Catalog=find;Integrated Security=True";
sms_conn = new SqlConnection(sms_connstr);
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
string sqlstr = "select * from ziliao where taname='" + TextBox1.Text + "'and taname='" + TextBox2.Text + "'and myname='" + TextBox4.Text + "'and mycity='" + TextBox5.Text + "'";
SqlCommand cmd = new SqlCommand(sqlstr, sms_conn);
sms_conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
Response.Write("here");
sms_conn.Close();
}
else
{
sms_sqlstr = "insert into ziliao(myname,mycity,email,taname,tacity)values(" + TextBox1.Text + "," + TextBox2.Text + "," + TextBox3.Text + "," + TextBox4.Text + "," + TextBox5.Text + ")";
SqlCommand cmd2 = new SqlCommand(sms_sqlstr, sms_conn);
sms_conn.Open();
cmd2.ExecuteNonQuery();
sms_conn.Close();
};
}
}
------解决方案--------------------
很混乱,sms_conn.Open应该只在一个地方调用,sms_conn.Close也是,
这个提示应该是在else分支内产生的,在SqlDataReader dr = cmd.ExecuteReader()之前就已经open过一次了,然后在else里又open一次,当然会出这个错误,
------解决方案--------------------
C# code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sms_conn;
string sms_sqlstr;
protected void Page_Load(object sender, EventArgs e)
{
string sms_connstr = "Data Source=20110721-1509\\sqlexpress;Initial Catalog=find;Integrated Security=True";
sms_conn = new SqlConnection(sms_connstr);
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
sms_conn.Open();
string sqlstr = "select * from ziliao where taname='" + TextBox1.Text + "'and taname='" + TextBox2.Text + "'and myname='" + TextBox4.Text + "'and mycity='" + TextBox5.Text + "'";
SqlCommand cmd = new SqlCommand(sqlstr, sms_conn);
SqlDataReader dr = cmd.ExecuteReader();
bool hd = dr.Read();
dr.Dispose();
if (hd)
{
Response.Write("here");
}
else
{
sms_sqlstr = "insert into ziliao(myname,mycity,email,taname,tacity)values(" + TextBox1.Text + "," + TextBox2.Text + "," + TextBox3.Text + "," + TextBox4.