我正在使用带有c#的asp.net,这些代码行中存在错误。
protected void btnsubmit_Click(object sender, EventArgs e)
{
string type = "c";
string FID = Session["FID"].ToString();
SqlConnection cn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
//int str_diff = Convert.ToInt32(ConfigurationManager.AppSettings["Difference"]);
cn.ConnectionString = @"Data Source=BOPSERVER;Initial Catalog=Project;Integrated Security=True";
cn.Open();
cmd.CommandText = "update TrackingFaculty_det SET Type=@Type WHERE (FID=@FID) and DATEDIFF(d,TrackingFaculty_det.LastUpdateDate,GETDATE())>60";
cmd.Parameters.Add("@FID",SqlDbType.VarChar,10);
cmd.Parameters["@FID"].Value = FID;
cmd.Parameters.Add("@Type", SqlDbType.VarChar, 1);
cmd.Parameters["@Type"].Value = type;
cmd.ExecuteNonQuery();
cn.Close();
Response.Redirect("~/Faculty/Personaldet.aspx");
}
答案 0 :(得分:4)
您尚未设置与命令的连接
cmd.Connection = cn;
答案 1 :(得分:2)
您需要将SqlConnection
分配给SqlCommand
。作为一个额外的建议,我会将连接包装在一个使用块中,以确保在异常的情况下正确处理它。
using (SqlConnection cn = new SqlConnection(@"Data Source=BOPSERVER;Initial Catalog=Project;Integrated Security=True")
{
cn.Open();
SqlCommand cmd = new SqlCommand("update TrackingFaculty_det SET Type=@Type WHERE (FID=@FID) and DATEDIFF(d,TrackingFaculty_det.LastUpdateDate,GETDATE())>60", cn);
cmd.Parameters.Add("@FID",SqlDbType.VarChar,10);
cmd.Parameters["@FID"].Value = FID;
cmd.Parameters.Add("@Type", SqlDbType.VarChar, 1);
cmd.Parameters["@Type"].Value = type;
cmd.ExecuteNonQuery();
}
答案 2 :(得分:0)
SqlCommand cmd = new SqlCommand("update TrackingFaculty_det SET Type=@Type WHERE (FID=@FID) and DATEDIFF(d,TrackingFaculty_det.LastUpdateDate,GETDATE())>60", cn);
cmd.Parameters.Add("@FID",SqlDbType.VarChar,10);
cmd.Parameters["@FID"].Value = FID;
cmd.Parameters.Add("@Type", SqlDbType.VarChar, 1);
cmd.Parameters["@Type"].Value = type;
cmd.ExecuteNonQuery();