0

我在 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");
}

错误!!!

4

3 回答 3

4

您尚未设置与命令的连接

cmd.Connection = cn;
于 2013-10-07T05:35:54.683 回答
2

您需要将 分配SqlConnectionSqlCommand. 作为附加建议,我会将连接包装在 using 块中,以确保在发生异常时正确处理它。

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();
}
于 2013-10-07T05:45:06.613 回答
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();                       
于 2013-10-07T05:39:11.557 回答