在对access数据库操作的时候,同样的sql语句,用程序就产生异常,说insert语句异常,可是把sql语句复制到access数据库编辑器运行的时候,又可以插入,请问这是为什么?
代码如下
:
Code
string conStr = "provider=microsoft.jet.oledb.4.0;data source=D:\\myfile\\My QQ Files\\DataBase\\sovo.mdb";
public int AddUser(user u)
{
OleDbConnection con = new OleDbConnection(conStr);
string sql = "insert into tbluser(name,password,sex,birthday,teacher,working,hobby,place,leave,leavephone,grade,class,qq,email,telephone,direction,regtime) values('" +u.Name + "','" + u.Password + "','" + u.Sex + "','" + u.Birthday + "','" + u.Teacher + "','" +u.Work + "','" + u.Hobby + "','" + u.Place + "','" + u.Leave + "','" + u.LeavePhone + "','" +u.Grade + "','" + u.Uclass + "','" + u.Qq + "','" + u.Email + "','" + u.TelePhone + "','" +u.Direction + "','" + u.RegTime.ToString() + "')";
OleDbCommand cmd = new OleDbCommand(sql, con);
int count = -1;
try
{
con.Open();
count = cmd.ExecuteNonQuery();
}
catch (OleDbException e)
{
count = -1;
string ss = e.ToString();
}
finally
{
con.Close();
}
return count;
}
这是获取到的sql语句,它提示出错
但是在access数据库中可以执行
Code
insert into tbluser(name,password,sex,birthday,teacher,working,hobby,place,leave,leavephone,grade,class,qq,email,telephone,direction,regtime) values('121','11','美女','qe24','2345','Flash ','为期二天','大四','万恶趣味','敖德萨','我而武器','去玩儿','阿斯顿擦拭的','344334@rewtr.com','完全全文','|助幼服务|助老服务|助贫服务|环保服务|社区服务|问卷调查|技能指导|救援服务|校园活动|','2008-11-9 18:01:10')
1,access 数据库字段最好用中括号括上 例如:
name 最好写成 [name]
2,日期要包含在“#”中
例如:
date>#2005-06-09#