即时通讯困惑如何做到这一点,因为我是相当新的c#访问sql服务器。 我想用时间在startTime和EndTime之间的条件查询表格测验。然而,当我尝试添加时间参数,它说我有一个空引用异常。System.NullReferenceException添加日期时间SQL参数
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<[email protected] and EndTime>[email protected] and Subject_ID = @subject", con);
com.ExecuteNonQuery();
con.Close();
'SqlCommand的COM = new SqlCommand(“Select * from quiz where StartTime <= @ time and EndTime> = @ time and Subject_ID = @subject”,con);'而不是'SqlCommand com; ' – fubo