即時通訊困惑如何做到這一點,因爲我是相當新的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