2014-09-19 39 views
0

這是我的查詢,組合日期和時間值並將其插入到數據庫在asp.net

string sdatevalue = mysdate.ToString("yyyy-MM-dd"); // "2014-02-12" 
string stime = txttime.Text; // 9.00 A.M 

replace 9.00 to 9:00 = correcttime; 


finaldate=sdatevalue + correcttime; 

如何實現這一目標。在asp.net C#做

謝謝,我想這

+0

你的_query_在哪裏?通常,使用正確類型的sql-parameters而不是帶格式化字符串的字符串連接。這可以防止幾個問題,最重要的是sql注入。 – 2014-09-19 13:19:20

+0

我使用的存儲過程已經是我的日期在表中只是想更新時間00:00:00作爲我的輸入時間 – BeginnerStack1 2014-09-19 13:24:19

回答

2

,你可能會被插入到數據庫中這不是一個正確的做法這一點,它的更好的你在你的數據庫本身。

創建方法或擴展

string ReplaceFirstOccurenceofCharacter(string text, string search, string replace) 
    { 
     int pos = text.IndexOf(search); 
     if (pos < 0) 
     { 
      return text; 
     } 
     return text.Substring(0, pos) + replace + text.Substring(pos + search.Length); 
    } 

並調用該方法

 string sdatevalue = "2014-02-12"; 
     string stime = "9.00 A.M"; 
     string concatinatedstring = string.Format("{0} {1}", sdatevalue, stime); 
     var result = ReplaceFirstOccurenceofCharacter(concatinatedstring, ".", ":"); 

實際的代碼是從This link拉希望它可以幫助你。

+0

夢幻般的感謝! – BeginnerStack1 2014-09-20 05:54:33

相關問題