2015-07-20 90 views
2

我找不到爲什麼我的第二個參數(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); 
} 
+0

什麼是NotifyDate對SQL表本身的數據類型? –

回答

0

那麼對於初學者嘗試做兩件事情

  1. NotifyDateParameter.ParameterName = "@NotifyDate"; // @added to parameter name更換NotifyDateParameter.ParameterName = "NotifyDate";
  2. 檢查參數類型是SqlDbType.DateTimeSqlDbType.Date
+0

將其更改爲「@NotifyDate」工作!謝謝。我只是好奇,爲什麼我不需要在「ImageData」之前添加「@」字符才能工作?這個參數一直沒有它... – Vigs

+0

@Vigs - 以前也發生過我。我無法找到背後的邏輯。 –

相關問題