简体   繁体   English

C#-ASP.NET-将日历中的日期插入数据库

[英]C# - ASP.NET - Inserting Date from a calendar into a DataBase

I am trying to store the date from a calendar which the user has selected into a database. 我试图将用户选择的日历中的日期存储到数据库中。 It seems to not being inserting it into the Access Database. 似乎没有将其插入到Access数据库中。 The DB field data type is Date/Time and the variables are below. DB字段数据类型为日期/时间,变量在下面。 I know my Query works because the other information stores correctly. 我知道我的查询有效,因为其他信息可以正确存储。

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+ "')";

Thanks 谢谢

try with 尝试

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

Note that your table column Date Joined having space, use [Date Joined] 请注意,您的表列“ Date Joined有空格,请使用[Date Joined]

Use Parameters as below 使用参数如下

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM