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();
答案 0 :(得分:0)
未分配Com,com为null。首先将参数添加到com,然后执行com = new 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 = null