我找不到爲什麼我的第二個參數(NotifyDateParameter
)對於我的SqlCommand
無法正常工作。它不會給出錯誤,但它在我的SQL Server數據庫中顯示爲空。第一個參數(StringParameter
)與預期一致。我現在可以利用你的一些專業知識。MVC:SqlCommand在數據庫中顯示爲空的第二個參數
try
{
{
string connstr = @"Server=.\SQLEXPRESS;Database=ImageDB;Trusted_Connection=True;";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string query;
byte[] fileData = null;
using (var binaryReader = new BinaryReader(Request.Files[upload].InputStream))
{
fileData = binaryReader.ReadBytes(Request.Files[upload].ContentLength);
}
query = "insert into Images(ImageData, NotifyDate) values(@ImageData, @NotifyDate)";
SqlParameter StringParameter = new SqlParameter();
StringParameter.SqlDbType = SqlDbType.VarBinary;
StringParameter.ParameterName = "ImageData";
StringParameter.Value = fileData;
DateTime today = DateTime.Now;
DateTime notifyDate = today.AddDays(1);
//string notifyDateString = notifyDate.ToString();
SqlParameter NotifyDateParameter = new SqlParameter();
NotifyDateParameter.SqlDbType = SqlDbType.DateTime;
NotifyDateParameter.ParameterName = "NotifyDate";
NotifyDateParameter.Value = notifyDate;
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(StringParameter);
cmd.Parameters.Add(NotifyDateParameter);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
conn.Dispose();
}
}
catch (Exception e)
{
string exceptionCause = String.Format("An error occurred: '{0}'", e);
System.IO.File.WriteAllText(@"C:\Users\Nathan\Documents\Visual Studio 2013\Projects\MVCImageUpload\uploads\exception.txt", exceptionCause);
}
什麼是NotifyDate對SQL表本身的數據類型? –