2013-11-28 83 views
0

我想從用戶選擇到數據庫中的日曆中存儲日期。 它似乎沒有將其插入到Access數據庫中。 DB字段的數據類型是日期/時間,變量如下。 我知道我的查詢工作正常,因爲其他信息存儲正確。C#ASP.NET - 從日曆插入日期到數據庫

DateTime dtUserDate; 
dtUserDate = calenderUserDate.SelectedDate; 

string myQuery = "INSERT INTO MMK(Name, Email, Address, Town, County, PostCode, Country, Telephone, Date Joined) VALUES ('" + strName + "' , '" + strEmail + "' , '" + strAddress + "' , '" + strTown + "' , '" + strCounty + "' , '" + strPostCode + "' , '" + strCountry + "' , '" + strTeleNumber + "' , '" + dtUserDate+ "')"; 

感謝

+1

「DAT e Joined「不是有效的字段名稱,這可能是問題,也可能只是其中的一部分 – musefan

+2

您需要閱讀[SQL注入](http://en.wikipedia.org/wiki/) SQL_injection),因爲你當前的代碼是一個安全問題 – musefan

回答

1

與具有空間的表列Date Joined

INSERT INTO MMK(Name, Email, Address, Town, County, PostCode, Country, Telephone, [Date Joined]) .... 

注試試,用[Date Joined]

使用參數如下

string myQuery = "INSERT INTO MMK(Name, Email, Address, Town, County, PostCode, Country, Telephone, [Date Joined]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
using (var cmd = new OleDbCommand(myQuery, con)) 
{ 
    cmd.Parameters.AddWithValue("Name", strName); 
    .... 
    cmd.Parameters.AddWithValue("DateJoined", dtUserDate); 
+0

非常感謝你[]括號工作:) – user3045496

+1

不錯,我想這樣的事情:'SqlCommand cmd = new SqlCommand(「INSERT INTO MMK(Name,Email,地址,城鎮,縣,郵政編碼,國家,電話,[加入日期])VALUES(@Name,@Email,@Address,@Town,@County,@PostCode,@Country,@TeleNumber,@UserDate,conn); cmd.Parameters.Add(「@ Name」,System.Data.SqlDbType.String).Value = strName; cmd.Parameters.Add(「@ UserDate」,System.Data.SqlDbType.DateTime).Value = calenderUserDate.SelectedDate;' – Dawid

+0

@Dawid OLE DB .NET Provider不支持命名參數,請檢查[文檔](http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters(v = vs.110).aspx) – Damith