2014-01-23 43 views
1

當我的存儲過程用於我的asp web表單時,我試圖讓它抓住當前的時間和日期。該信息將存儲在具有datetime2數據類型的列中。在填寫自己的狀態,我得到這個..int與datetime2不兼容

操作數類型衝突:int是不相容的DATETIME2

command.Parameters.AddWithValue("@dt2LastLoginDate", SqlDbType.DateTime2); 

我應該爲了存儲日期和時間爲正確的數據類型可以用嗎?

protected void Submit_Click(object sender, EventArgs e) 
{ 
    try 
    { 

     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegDNMembershipConnectionString"].ConnectionString); 
     con.Open(); 
     SqlCommand command = new SqlCommand("dbo.P_AddAccount"); 
     command.Connection = con; 
     command.CommandType = CommandType.StoredProcedure; 
     command.Parameters.Add(new SqlParameter("@nvcAccountName", TextBoxUN.Text)); 
     command.Parameters.Add(new SqlParameter("@inyAccountLevelCode", 100)); 
     command.Parameters.Add(new SqlParameter("inyCharacterCreateLimit", 4)); 
     command.Parameters.Add(new SqlParameter("@inyCharacterMaxCount", 4)); 
     var param = new SqlParameter("@dt2LastLoginDate", System.Data.SqlDbType.DateTime2); 
     param.Value = DateTime.Now; 
     command.Parameters.Add(param); 
     var wrongParam = new SqlParameter("@dt2LastLoginDate", System.Data.SqlDbType.DateTime2); 
     wrongParam.Value = System.Data.SqlDbType.DateTime2; 
     command.Parameters.Add(new SqlParameter("@vchLastLoginIP", null)); 
     command.Parameters.Add(new SqlParameter("@@IntLastSessionID", null)); 
     command.Parameters.Add(new SqlParameter("@vchJoinIP", null)); 
     command.Parameters.Add(new SqlParameter("@inyPublisherCode", 4)); 
     command.Parameters.Add(new SqlParameter("@inyGenderCode", null)); 
     command.Parameters.Add(new SqlParameter("@DaTBirthDate", null)); 
     command.Parameters.Add(new SqlParameter("@vchPassphrase", TextBoxPass.Text)); 
     command.Parameters.Add(new SqlParameter("@inyNationalityCode", null)); 
     command.Parameters.Add(new SqlParameter("@inyChannelPartnerCode", null)); 
     command.Parameters.Add(new SqlParameter("@EmailAddress", TextBoxEA.Text)); 
     command.Parameters.Add(new SqlParameter("@FullName", TextBoxFN.Text)); 
     command.Parameters.Add(new SqlParameter("@Country", DropDownListCountry.SelectedValue)); 
     command.ExecuteNonQuery(); 
     con.Close(); 


    } 

回答

1

SqlDBType.DataTime2MSDN reference)是Enum這將被轉換爲執行參數時int(值33)。您需要提供該設置的實際DateTime值,您希望設置爲執行參數。示例應該是:

var param = new SqlParameter("@dt2LastLoginDate", System.Data.SqlDbType.DateTime2); 
param.Value = System.DateTime.Now; 
command.Parameters.Add(param); 

這會創建一個新的參數,其參數名稱和數據類型。接下來,我們將參數的值設置爲DateTime.Now或您選擇的日期值。最後將這個添加到參數集合中。

現在在您的示例中,Parameters.AddWithValue()方法接受兩個參數。參數的名稱(即@dt2LastLoginDate)和在執行期間將參數設置爲的值。因此,您可以重寫這個聲明可以被改寫爲:

var wrongParam = new SqlParameter("@dt2LastLoginDate", System.Data.SqlDbType.DateTime2); 
wrongParam.Value = System.Data.SqlDbType.DateTime2; 

正如你所看到的值設置爲的SqlDbType.DateTime2

+0

枚舉值當我兩個片段添加到我的代碼吧'DateTime'是「方法」在給定的上下文中無效。這是爲什麼? – user3219150

+0

你絕對不需要這兩個片段。只需使用第一個,第二個只是顯示錯誤發生的原因。現在對於你的DateTime方法問題,你可能會重寫標準的DateTime變量。將'param.Value = DateTime.Now;'改爲'param.Value = System.DateTime.Now;' – Nico

相關問題