我正在用asp.net做一个网页,网页上我添加了日历,我想从日历上读出时间,再将这个时间添加到access数据库相应的项中,我的access数据库中相应项也是日期时间行的数据。但是运行时出现错误说“标准表达式中数据类型不匹配”。我把修改数据库的原码贴在下面了:DateTime buyTime = orderCalendar.SelectedDate.Date;
DateTime expireTime = expireCalendar.SelectedDate.Date;
string IDText = IDTextBox.Text;
......
......string command = "insert into datetable(ID,buydate,expiredate) values ('"+IDText+"', 'buyTime', 'expireTime')";我想应该是command中的格式不对,大家帮我看看应该怎么改呀?另外我想知道,command语句写access数据库和写sql server是不是一样呢?那个好心人如果知道写时间日期到sql server中的格式也麻烦告我一下行吗?很急很急,多谢多谢

解决方案 »

  1.   

    string command = "insert into datetable(ID,buydate,expiredate) values ('"+IDText+"', 'buyTime', 'expireTime')";
                  ^^^^^^^^^^^  ^^^^^^^^^^^^^
    ???the worst you can do isstring command = "insert into datetable(ID,buydate,expiredate) values ('"+IDText+"', '" + buyTime.ToString() + "', '" + expireTime.ToString() + "')";but you can dostring command = String.Format("insert into datetable(ID,buydate,expiredate) values ('{0}', '{1}','{2}')", IDText, buyTime, expireTime);or you should do (for System.Data.SqlClient)SqlConnection conn = new SqlConnection(".....");
    string command = "insert into datetable(ID,buydate,expiredate) values (@ID, @buydate, @expiredate)";
    SqlCommand cmd = new SqlCommand(command, conn);
    cmd.Parameters.Add("@ID", IDText);
    cmd.Parameters.Add("@buydate", buytime);
    cmd.Parameters.Add("@expiredate", expiretime);conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
      

  2.   

    thank you so much, you are really very very helpful.....;)