我对如何做到这一点感到困惑,因为我对通过c#访问server非常陌生。我想用startTime和EndTime之间的时间的条件来查询表测试。但是,当我试图添加时间参数时,它会说我有一个空引用异常。
DateTime date = DateTime.Now;
SqlConnection con = new SqlConnection("Data Source=.\\SQLSERVER;Initial Catalog=quizMaker;Integrated Security=True");
SqlCommand com;
subjects = "Subject-3";
con.Open();
SqlParameter time = new SqlParameter("@time", SqlDbType.DateTime);
time.Value = date;
com.Parameters.Add(time); //error pops up here
SqlParameter subjected = new SqlParameter("@subject", SqlDbType.VarChar, 20);
subjected.Value = subjects;
com = new SqlCommand("Select * from quiz where StartTime<=@time and EndTime>=@time and Subject_ID = @subject", con);
com.ExecuteNonQuery();
con.Close();发布于 2015-10-07 12:05:33
Com未分配,com为null。首先将参数添加到com中,然后执行com =新SqlCommand
com.Parameters.Add(time); //<-- com = null
com = new SqlCommand("Select * from quiz where StartTime<=@time and EndTime>=@time and Subject_ID = @subject", con); //<-- com has a value (!= null)它只需做其他明智的事情:地点
com = new SqlCommand("Select * from quiz where StartTime<=@time and EndTime>=@time and Subject_ID = @subject", con); //<-- com has a value (!= null)在此之前
com.Parameters.Add(time); //<-- com = nullhttps://stackoverflow.com/questions/32991716
复制相似问题