在使用C#操纵msaccess时需要帮助 [英] need help on manipulating msaccess using c#
问题描述
try
{
OleDbConnection conn = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|OCSRSRecord.mdb;Persist Security Info=True");
conn.Open();
OleDbCommand cmd = new OleDbCommand("INSERT INTO USERS VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", conn);
cmd.Parameters.Add("user_id", OleDbType.VarChar);
cmd.Parameters.Add("password", OleDbType.VarChar);
cmd.Parameters.Add("levelType", OleDbType.VarChar);
cmd.Parameters.Add("user_type", OleDbType.VarChar);
cmd.Parameters.Add("user_lastName", OleDbType.VarChar);
cmd.Parameters.Add("user_firstName", OleDbType.VarChar);
cmd.Parameters.Add("user_middleName", OleDbType.VarChar);
cmd.Parameters.Add("user_gender", OleDbType.VarChar);
cmd.Parameters.Add("user_civilStatus", OleDbType.VarChar);
cmd.Parameters.Add("user_birthday", OleDbType.Date);
cmd.Parameters.Add("user_address", OleDbType.VarChar);
cmd.Parameters.Add("user_emailAddress", OleDbType.VarChar);
if (passBox.Text.Equals(confirmPassBox.Text))
{
cmd.Parameters["user_id"].Value = userIDBox.Text;
cmd.Parameters["password"].Value = passBox.Text;
cmd.Parameters["user_type"].Value = userTypeBox.Text;
cmd.Parameters["user_lastName"].Value = lastNameBox.Text;
cmd.Parameters["user_firstName"].Value = firstNameBox.Text;
cmd.Parameters["user_middleName"].Value = middleNameBox.Text;
cmd.Parameters["user_gender"].Value = genderBox.Text;
cmd.Parameters["user_civilStatus"].Value = civilStatusBox.Text;
cmd.Parameters["user_birthday"].Value = DateTime.Parse(birthDateBox.Text);
cmd.Parameters["user_address"].Value = addressBox.Text;
cmd.Parameters["user_emailAddress"].Value = emailBox.Text;
cmd.Parameters["levelType"].Value = teachTypeBox.Text;
cmd.ExecuteNonQuery();
MessageBox.Show("Account " + userIDBox.Text + " saved", "Account Creation Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
this.Close();
}
else
{
MessageBox.Show("Password did not match", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
这是我的代码...我认为它是有效的,因为当我在USERS表上创建一个新帐户时,以及当我注销并使用我创建的新帐户登录到我的应用程序时,它都可以工作...但是问题是当我打开msaccess数据库时,我看不到我创建的新记录...并且当我尝试使用我创建的新帐户再次登录我的应用程序时,它不再起作用...
根据我的观察.每次我打开msaccess数据库时,我创建的新帐户都会消失...请帮助:(
Here is my code... and i think it is working because when i create a new account on my USERS table, and when i logout and use the new account that i created to login into my application, it works... but the problem is when i open my msaccess database, i dont see the new record i created... and when i tried to to login again on my application using my new created account, it does not work anymore...
base on my observation. Everytime i open my msaccess database, my new accounts that i created disappears... please help :(
推荐答案
我建议对代码进行一些改进.... ....
代替这个.....
I would suggest some improvements in the code........
Instead of this .....
OleDbCommand cmd = new OleDbCommand("INSERT INTO USERS VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", conn);
尝试使用
try using
OleDbCommand cmd = new OleDbCommand("INSERT INTO USERS(col1,col2,col3,col4,col5) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", conn);
col1是表中实际列的名称.
您可以使用...来确认是否在表中插入了记录...
col1 are name of actual columns in tables.......
You can confirm if record was inserted in table by using.........
int i=cmd.ExecuteNonQuery();
if(i>0)
{
MessageBox.Show("Record Added");
}
而不是像两次一样使用参数.......
Instead of using parameter two times like.......
cmd.Parameters.Add("user_id", OleDbType.VarChar);
cmd.Parameters["user_id"].Value = userIDBox.Text;
如果您喜欢我太懒了,请使用以下内容,从而减少您的编码工作量和时间..... ;-)
use the following thus reduce you coding effort and time if you like I m too lazy.....;-)
cmd.Parameters.Add("user_id", OleDbType.VarChar).Value=userIDBox.Text;
祝您好运……
Best of luck......
我已经尝试过您的解决方案了,先生...发生错误,并说"INSERT INTO语句中的语法错误" ...我认为它的列部分...顺便说一句,谢谢您的代码提示:)
曼图·辛格先生,您认为问题出在我的msaccess数据库上吗?
i''ve tried your solution sir... an error occur and it says "Syntax error in INSERT INTO statement"... i think its the column part... btw, thanks for the less code tip :)
sir Mantu Singh, do you think the problem is on my msaccess database?
这篇关于在使用C#操纵msaccess时需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!