我已尽力解决它,但我做不到。请帮助 me.The 连接未关闭。连接的当前状态是打开的
I tried enough to solve it, but i can't. Please Help me.The connection was not closed. The connection's current state is open
我正在尝试在我的数据库中插入日期(从和到),试试这个代码,但它总是向我显示这个错误:
The connection was not closed. The connection's current state is open.
当我删除中间的 con.Open() 时,它抛出异常:
ExecuteReader: Connection property has not been initialized.
private void btnDiff_Click(object sender, EventArgs e)
{
DateTime oldDate = DateTime.Parse(txtFirstDate.Text);
DateTime fromdate = oldDate;
DateTime todate = oldDate;
SqlCommand cmd1 = new SqlCommand("SELECT [Topic_ID] FROM [CourseDB].[dbo].[Topic]", con);
con.Open();
SqlDataReader reader = cmd1.ExecuteReader();
while (reader.Read())
{
string column = reader["Topic_ID"].ToString();
int a = Convert.ToInt32(reader["Topic_ID"]);
fromdate = todate.AddDays(0);
todate = fromdate.AddDays(7);
SqlCommand cmd = new SqlCommand();
con.Open();
SqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE [CourseDB].[dbo].[Topic] SET [From_Date]='" + fromdate.ToString() + "', [To_Date]='" + todate.ToString() + "' WHERE [Topic_ID]='" + a + "'";
cmd.ExecuteNonQuery();
MessageBox.Show("Dates Inserted");
}
r.Close();
con.Close();
}
reader.Close();
con.Close();
}
我不明白你用下面的代码在做什么。
SqlCommand cmd = new SqlCommand();
con.Open();
SqlDataReader r = cmd.ExecuteReader();
cmd要执行什么命令。没有指定任何命令文本。
根据我的说法,您应该建立单独的连接 class,这有助于您正确维护代码。
public class AppConnection
{
public string ConnectionString
{
get { return ConfigurationManager.ConnectionStrings["WebConfigConnectionStringName"].ConnectionString; }
}
public SqlConnection MakeConnection()
{
SqlConnection newConnection = new SqlConnection(this.ConnectionString);
if (newConnection.State == ConnectionState.Closed)
{
newConnection.Open();
}
return newConnection;
}
public SqlDataReader ExecCommand(string commandText, CommandType commandType, ref SqlConnection appConnection)
{
SqlDataReader reader;
using (SqlCommand dbCommand = new SqlCommand())
{
dbCommand.CommandText = commandText;
dbCommand.CommandType = commandType;
dbCommand.Connection = appConnection;
reader = dbCommand.ExecuteReader();
}
return reader;
}
}
按钮点击事件可能是这样的:-
private void btnDiff_Click(object sender, EventArgs e)
{
DateTime oldDate = DateTime.Parse(txtFirstDate.Text);
DateTime fromdate = oldDate;
DateTime todate = oldDate;
AppConnection appConnection = new AppConnection();
SqlConnection con = appConnection.MakeConnection();
SqlDataReader readerTopics = appConnection.ExecCommand("SELECT [Topic_ID] FROM [CourseDB].[dbo].[Topic]", CommandType.Text, ref con);
while (readerTopics.Read())
{
string column = readerTopics["Topic_ID"].ToString();
int a = Convert.ToInt32(readerTopics["Topic_ID"]);
fromdate = todate.AddDays(0);
todate = fromdate.AddDays(7);
using (SqlCommand updateCommand = new SqlCommand())
{
updateCommand.CommandText = "UPDATE [CourseDB].[dbo].[Topic] SET [From_Date]='" + fromdate.ToString() + "', [To_Date]='" + todate.ToString() + "' WHERE [Topic_ID]='" + a + "'";
updateCommand.Connection = con;
updateCommand.CommandType = CommandType.Text;
updateCommand.ExecuteNonQuery();
}
}
}
我就是这么做的,我的问题就解决了:)
DateTime oldDate = DateTime.Parse(txtFirstDate.Text);
DateTime fromdate = oldDate;
DateTime todate = oldDate;
SqlCommand cmd1 = new SqlCommand("SELECT [Topic_ID] FROM [CourseDB].[dbo].[Topic]", con);
con.Open();
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd1);
adapter.Fill(dt);
foreach (DataRow row in dt.Rows)
{
string column = row["Topic_ID"].ToString();
int a = Convert.ToInt32(row["Topic_ID"]);
fromdate = todate.AddDays(0);
todate = fromdate.AddDays(7);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE [CourseDB].[dbo].[Topic] SET [From_Date]='" + fromdate.ToString() + "', [To_Date]='" + todate.ToString() + "' WHERE [Topic_ID]='" + a + "'";
cmd.ExecuteScalar();
this.topicTableAdapter.Fill(this.courseDataSet.Topic);
我正在尝试在我的数据库中插入日期(从和到),试试这个代码,但它总是向我显示这个错误:
The connection was not closed. The connection's current state is open.
当我删除中间的 con.Open() 时,它抛出异常:
ExecuteReader: Connection property has not been initialized.
private void btnDiff_Click(object sender, EventArgs e)
{
DateTime oldDate = DateTime.Parse(txtFirstDate.Text);
DateTime fromdate = oldDate;
DateTime todate = oldDate;
SqlCommand cmd1 = new SqlCommand("SELECT [Topic_ID] FROM [CourseDB].[dbo].[Topic]", con);
con.Open();
SqlDataReader reader = cmd1.ExecuteReader();
while (reader.Read())
{
string column = reader["Topic_ID"].ToString();
int a = Convert.ToInt32(reader["Topic_ID"]);
fromdate = todate.AddDays(0);
todate = fromdate.AddDays(7);
SqlCommand cmd = new SqlCommand();
con.Open();
SqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE [CourseDB].[dbo].[Topic] SET [From_Date]='" + fromdate.ToString() + "', [To_Date]='" + todate.ToString() + "' WHERE [Topic_ID]='" + a + "'";
cmd.ExecuteNonQuery();
MessageBox.Show("Dates Inserted");
}
r.Close();
con.Close();
}
reader.Close();
con.Close();
}
我不明白你用下面的代码在做什么。
SqlCommand cmd = new SqlCommand();
con.Open();
SqlDataReader r = cmd.ExecuteReader();
cmd要执行什么命令。没有指定任何命令文本。
根据我的说法,您应该建立单独的连接 class,这有助于您正确维护代码。
public class AppConnection
{
public string ConnectionString
{
get { return ConfigurationManager.ConnectionStrings["WebConfigConnectionStringName"].ConnectionString; }
}
public SqlConnection MakeConnection()
{
SqlConnection newConnection = new SqlConnection(this.ConnectionString);
if (newConnection.State == ConnectionState.Closed)
{
newConnection.Open();
}
return newConnection;
}
public SqlDataReader ExecCommand(string commandText, CommandType commandType, ref SqlConnection appConnection)
{
SqlDataReader reader;
using (SqlCommand dbCommand = new SqlCommand())
{
dbCommand.CommandText = commandText;
dbCommand.CommandType = commandType;
dbCommand.Connection = appConnection;
reader = dbCommand.ExecuteReader();
}
return reader;
}
}
按钮点击事件可能是这样的:-
private void btnDiff_Click(object sender, EventArgs e)
{
DateTime oldDate = DateTime.Parse(txtFirstDate.Text);
DateTime fromdate = oldDate;
DateTime todate = oldDate;
AppConnection appConnection = new AppConnection();
SqlConnection con = appConnection.MakeConnection();
SqlDataReader readerTopics = appConnection.ExecCommand("SELECT [Topic_ID] FROM [CourseDB].[dbo].[Topic]", CommandType.Text, ref con);
while (readerTopics.Read())
{
string column = readerTopics["Topic_ID"].ToString();
int a = Convert.ToInt32(readerTopics["Topic_ID"]);
fromdate = todate.AddDays(0);
todate = fromdate.AddDays(7);
using (SqlCommand updateCommand = new SqlCommand())
{
updateCommand.CommandText = "UPDATE [CourseDB].[dbo].[Topic] SET [From_Date]='" + fromdate.ToString() + "', [To_Date]='" + todate.ToString() + "' WHERE [Topic_ID]='" + a + "'";
updateCommand.Connection = con;
updateCommand.CommandType = CommandType.Text;
updateCommand.ExecuteNonQuery();
}
}
}
我就是这么做的,我的问题就解决了:)
DateTime oldDate = DateTime.Parse(txtFirstDate.Text);
DateTime fromdate = oldDate;
DateTime todate = oldDate;
SqlCommand cmd1 = new SqlCommand("SELECT [Topic_ID] FROM [CourseDB].[dbo].[Topic]", con);
con.Open();
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd1);
adapter.Fill(dt);
foreach (DataRow row in dt.Rows)
{
string column = row["Topic_ID"].ToString();
int a = Convert.ToInt32(row["Topic_ID"]);
fromdate = todate.AddDays(0);
todate = fromdate.AddDays(7);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE [CourseDB].[dbo].[Topic] SET [From_Date]='" + fromdate.ToString() + "', [To_Date]='" + todate.ToString() + "' WHERE [Topic_ID]='" + a + "'";
cmd.ExecuteScalar();
this.topicTableAdapter.Fill(this.courseDataSet.Topic);