在C#,使用Access数据库无法插入记录?下面的语句是增加一个用户,ConstClass.databaseConnect是连接一个Access数据库。
系统执行myCommand.CommandText=@"select * from usertable where UserName='"+username+@"'";没有问题
但是执行Insert语句时系统出现Insert语法错误,是不是不支持Insert语句?
public bool AddUser(string username,string password,string confirmPassword)
{
if (password!=confirmPassword)
{
MessageBox.Show("The Password don't match, please try again! ");
return false;
} try
{
ConstClass.databaseConnect.Open();
OleDbCommand myCommand=new OleDbCommand();
myCommand.Connection=ConstClass.databaseConnect;
//Access 不区分大小写
myCommand.CommandText=@"select * from usertable where UserName='"+
username+@"'";
OleDbDataReader reader=myCommand.ExecuteReader(); if ( reader.HasRows ) {
MessageBox.Show("The user '"+username+"' already exists, please try again! ");
return false;
} reader.Close(); myCommand.CommandText="Insert into usertable (UserName,Password) Values ('"
+username
+"','"
+password
+"')";
MessageBox.Show(myCommand.CommandText); int row=myCommand.ExecuteNonQuery(); if (row>=1)
{
return true;
}
} catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
ConstClass.databaseConnect.Close();
}
return false;
}
系统执行myCommand.CommandText=@"select * from usertable where UserName='"+username+@"'";没有问题
但是执行Insert语句时系统出现Insert语法错误,是不是不支持Insert语句?
public bool AddUser(string username,string password,string confirmPassword)
{
if (password!=confirmPassword)
{
MessageBox.Show("The Password don't match, please try again! ");
return false;
} try
{
ConstClass.databaseConnect.Open();
OleDbCommand myCommand=new OleDbCommand();
myCommand.Connection=ConstClass.databaseConnect;
//Access 不区分大小写
myCommand.CommandText=@"select * from usertable where UserName='"+
username+@"'";
OleDbDataReader reader=myCommand.ExecuteReader(); if ( reader.HasRows ) {
MessageBox.Show("The user '"+username+"' already exists, please try again! ");
return false;
} reader.Close(); myCommand.CommandText="Insert into usertable (UserName,Password) Values ('"
+username
+"','"
+password
+"')";
MessageBox.Show(myCommand.CommandText); int row=myCommand.ExecuteNonQuery(); if (row>=1)
{
return true;
}
} catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
ConstClass.databaseConnect.Close();
}
return false;
}
是关键字,我也遇过这样的问题,折磨的不行,后来改了就好了。
请问password 是Access的关键字,还是所有数据库的关键字,请问数据库中还有哪些关键字不能作为字段名,在我的记忆中Memo好象也不能作为字段名
2.不太可能是权限不够,不要花时间在这个上面,因为如果你的权限不够,系统的报错会是“你需要一个可更新的视图”。如果你碰见这种情况,呵呵,最简单的方法就是把你的程序从 \Inetpub\wwwroot目录中移出来,因为这个目录里的文件要在iis中设置读写权限,比较麻烦。重新设置虚拟路径比较容易。
3.我大概看了你的程序,判断密码相同的时候时候用CompareValidiator会更简单.MSDN有详细的使用方法。
4.出现无法插入的情况,你可以试试只插一个字段试试,就能找出具体的出错位置了。