access中带参数sql语句问题~~标准表达式中数据类型不匹配

在Access中出错的代码:

string insertString = "insert into [tb_contactus] ([Name],[Mail],[Contact],[Description],[Date]) values(@Name,@Mail,@Contact,@Description,@Date)";
        OleDbCommand cmd = new OleDbCommand(insertString,con);
        cmd.Parameters.Add(new OleDbParameter("@Name", OleDbType.Char));
        cmd.Parameters["@Name"].Value = a;
        cmd.Parameters.Add(new OleDbParameter("@Mail", OleDbType.Char));
        cmd.Parameters["@Mail"].Value = b;
        cmd.Parameters.Add(new OleDbParameter("@Contact", OleDbType.Char));
        cmd.Parameters["@Contact"].Value = c;
        cmd.Parameters.Add(new OleDbParameter("@Description", OleDbType.Char));
        cmd.Parameters["@Description"].Value = d;
        cmd.Parameters.Add(new OleDbParameter("@Date", OleDbType.DBTimeStamp));
        cmd.Parameters["@Date"].Value = System.DateTime.Now;
        cmd.ExecuteScalar();
        cmd.Connection.Close();
        return cmd;

每次到cmd.ExecuteScalar()时就报错:标准表达式中数据类型不匹配

后来改成:

string insertString = "insert into [tb_contactus] ([Name],[Mail],[Contact],[Description],[Date]) values(@Name,@Mail,@Contact,@Description,now())";
        OleDbCommand cmd = new OleDbCommand(insertString,con);
        cmd.Parameters.Add(new OleDbParameter("@Name", OleDbType.Char));
        cmd.Parameters["@Name"].Value = a;
        cmd.Parameters.Add(new OleDbParameter("@Mail", OleDbType.Char));
        cmd.Parameters["@Mail"].Value = b;
        cmd.Parameters.Add(new OleDbParameter("@Contact", OleDbType.Char));
        cmd.Parameters["@Contact"].Value = c;
        cmd.Parameters.Add(new OleDbParameter("@Description", OleDbType.Char));
        cmd.Parameters["@Description"].Value = d;
        cmd.ExecuteScalar();
        cmd.Connection.Close();
        return cmd;

就没错了。

具体原因还不是很清楚,哪位明白的能指点一下吗?谢谢!!!

posted on 2008-07-29 23:17 Anything.NET 阅读(...) 评论(...) 编辑 收藏

导航

统计

公告