我正在尝试在我的数据库中插入日期(从&到),尝试此代码,但它总是向我显示此错误:
连接未关闭。连接的当前状态是打开的。
当我删除中间con.Open()时,它会抛出异常:
ExecuteReader:尚未初始化Connection属性。
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();
}
答案 0 :(得分:0)
我不能通过以下代码了解你在做什么。
SqlCommand cmd = new SqlCommand();
con.Open();
SqlDataReader r = cmd.ExecuteReader();
cmd将执行什么命令。没有指定任何命令文本。
根据我的说法,你应该建立单独的连接类,这有助于你正确地维护代码。
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();
}
}
}
答案 1 :(得分:0)
我只是这样做,&我的问题解决了:))
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);