2013-11-28 85 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