我试图拥有一个用户注册,这一切都很有效,并且新用户默认为用户而不是管理员。我正在使用.MDB来存储所有内容。
是否可以在我的初始sql查询中执行此操作? 目前我的代码看起来像
strSQL = "Insert into tblUserLogin " +
"(UserFirstName, UserLastName, UserName, UserPassword, UserAddress, UserCity, UserState, UserZipCode, UserEmail, UserPhone, ) values ('" +
UserFirstName + "', '" + UserLastName + "', '" + UserName + "', '" + UserPassword + "', '" + UserAddress +
"', '" + UserCity + "', '" + UserState + "', '" + UserZipCode + "', '" + UserEmail + "', '" + UserPhone +
"')";
// set the command text of the command object
command.CommandType = CommandType.Text;
command.CommandText = strSQL;
// Execute the insert statement
command.ExecuteNonQuery();
这会成功将所有内容发布到.MDB,它会将SecurityLevel列下的单元格留空。
我试图添加
"(UserFirstName, UserLastName, UserName, UserPassword, UserAddress, UserCity, UserState, UserZipCode, UserEmail, UserPhone, SecurityLevel=U )
但这并不像我希望的那样有效。是否有一种简单的方法可以将SecurityLevel的值默认为U而无需任何人指定它?
谢谢你的时间答案 0 :(得分:0)
如果我理解正确,只需对'U'
列使用const值SecurityLevel
strSQL = "INSERT INTO tblUserLogin " +
"(UserFirstName, UserLastName, " +
" UserName, UserPassword, " +
" UserAddress, UserCity, " +
" UserState, UserZipCode, " +
" UserEmail, UserPhone, SecurityLevel) " +
"VALUES (" +
UserFirstName + "', '" + UserLastName + "', '" +
UserName + "', '" + UserPassword + "', '" +
UserAddress + "', '" + UserCity + "', '" +
UserState + "', '" + UserZipCode + "', '" +
UserEmail + "', '" + UserPhone + "', 'U')"; // pass a const value for SecurityLevel column
// the rest of your code goes here
旁注:您的代码容易受到sql注入攻击。学习并使用预准备(参数化)语句,而不是动态构建查询字符串。