您好,我有一个方法可以在 SQL 中保存一些数据,但有时并不是每个参数都设置好了,这给了我一个例外,因为它的 NULL。
这是我的方法:
public string getBookingURL(string guid, BookingRequest request, string token, out string exitURL)
{
if (validateToken(token))
{
string command = "INSERT INTO OUTLOOKADDIN (GUID, TOKEN, BOOKINGID, STARTUTC, ENDUTC, SUBJECT, NUMPARTICIPANTS) VALUES (@GUID, @TOKEN, @BOOKINGID, @STARTUTC, @ENDUTC, @SUBJECT, @NUMPARTICIPANTS)";
SqlConnection connection = new SqlConnection(GetConnectionString());
connection.Open();
try
{
SqlCommand cmd = new SqlCommand(command, connection);
cmd.Parameters.Add("@GUID", guid);
cmd.Parameters.Add("@TOKEN", token);
cmd.Parameters.Add("@BOOKINGID", request.bookingID);
cmd.Parameters.Add("@STARTUTC", request.startUTC);
cmd.Parameters.Add("@ENDUTC", request.endUTC);
cmd.Parameters.Add("@SUBJECT", request.subject);
cmd.Parameters.Add("@NUMPARTICIPANTS", request.numParticipants);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cmd.Dispose();
cmd = null;
}
catch (Exception ex)
{
log.Error(ex.Message + "\n\rStackTrace:\n\r" + ex.StackTrace);
}
finally
{
connection.Close();
}
HttpContext current = HttpContext.Current;
string baseUrl = current.Request.Url.Scheme + "://"
+ current.Request.Url.Authority
+ current.Request.ApplicationPath.TrimEnd('/');
exitURL = baseUrl + "/index.html";
return baseUrl + '/'
+ "WebPage/Booking/BBooking.aspx?"
+ "OPS"
+ guid; ;
}
exitURL = null;
return null;
}
我认为这个 SQL 查询...
string command = "INSERT INTO OUTLOOKADDIN (GUID, TOKEN, BOOKINGID, STARTUTC, ENDUTC, SUBJECT, NUMPARTICIPANTS) 值 (@GUID, @TOKEN, @BOOKINGID, @STARTUTC, @ENDUTC, @SUBJECT, @NUMPARTICIPANTS)";
...应该动态运行还是有任何其他解决方案可以防止在参数为空时添加参数?