我正在读取Registry key
值并将它们存储在中string variables
,并将这些值作为参数传递到NewReg
方法中以将它们存储到数据库中,但是我收到此错误消息you must use an updateable query
,因此收到此消息user not added in database
,
我哪里错了?我怎样才能做到这一点?
public partial class LoginForm : Form
{
DBHandling db = new DBHandling();
public LoginForm()
{
try
{
RegistryKey key = Registry.ClassesRoot;
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\StudentExm");
string usrname = registryKey.GetValue("UserName").ToString();
string password = registryKey.GetValue("Password").ToString();
string emailId = registryKey.GetValue("EmailID").ToString();
string contctno = registryKey.GetValue("ContactNo").ToString();
string regDate = registryKey.GetValue("RegDate").ToString();
registryKey.Close();
if (NewReg(usrname, password, emailId, contctno, regDate))
{
MessageBox.Show("User Name: " + usrname + "\n" +
"Password: " + password + "\n" +
"Email ID: " + emailId + "\n" +
"Contact No: " + contctno + "\n" +
"Reg Date: " + regDate);
}
else
{
MessageBox.Show("User not added in the database.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
DBHandling 类中的 NewReg 方法
public bool NewReg(string usrName, string passwrd, string emailID, string contactNo,string regdat)
{
bool flag = false;
try
{
//string sql = "INSERT INTO test(nametest,pass,email,contact,regdate) Values('" + usrName + "','" + passwrd + "','" + emailID + "','" + contactNo + "','" + System.DateTime.Now + "')";
string sql = "INSERT INTO UserInfo([UserName],[Password],[email],[contact],[regdate]) Values(@UserInfo,@Password,@email,@contact,@regdate)";
cmd = new OleDbCommand(sql, acccon);
cmd.Parameters.AddRange(new[] {
new OleDbParameter("@UserName", usrName),
new OleDbParameter("@Password", passwrd),
new OleDbParameter("@EmailID", emailID),
new OleDbParameter("@ContactNo", contactNo),
new OleDbParameter("@RegDate", regdat)
});
cmd.ExecuteNonQuery();
flag = true;
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}
return flag;
}
提前感谢您的帮助