C#和msaccess中的INSERT语句问题... [英] INSERT statement problem in c# and msaccess...

查看:94
本文介绍了C#和msaccess中的INSERT语句问题...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序正在运行,但在ms-access端出现问题...我使用我的应用程序为我的登录应用程序创建了一个新的登录帐户...并且它可以工作,因为我可以使用自己创建的新登录帐户...问题是每次我打开我的ms-access文件时,我创建的新登录帐户都不会显示...并且当我尝试使用我创建的该登录帐户打开ms-access文件后尝试登录时,它没有工作了...

请帮助:(

这是我的代码...(我在代码上看不到任何问题,我在想我的ms-access文件是否有问题...)

the program is working but im having problems on the ms-access side... I created a new login account for my login application using my application... and it works because i can use my new login account that i created... the problem is everytime i open my ms-access file, the new login account i created doesnt show up... and when i try to login after i open the ms-access file using that login account i created, it doesnt work anymore...

PLEASE HELP :(

Here is my code... (I dont see any problems on my code, and i was thinking if there is something wrong with my ms-access file...)

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);

                if (passBox.Text.Equals(confirmPassBox.Text))
                {
                cmd.Parameters.Add("user_id", OleDbType.VarChar).Value = userIDBox.Text;
                cmd.Parameters.Add("password", OleDbType.VarChar).Value = passBox.Text;
                cmd.Parameters.Add("levelType", OleDbType.VarChar).Value = teachTypeBox.Text;
                cmd.Parameters.Add("user_type", OleDbType.VarChar).Value = userTypeBox.Text;
                cmd.Parameters.Add("user_lastName", OleDbType.VarChar).Value = lastNameBox.Text;
                cmd.Parameters.Add("user_firstName", OleDbType.VarChar).Value = firstNameBox.Text;
                cmd.Parameters.Add("user_middleName", OleDbType.VarChar).Value = middleNameBox.Text;
                cmd.Parameters.Add("user_gender", OleDbType.VarChar).Value = genderBox.Text;
                cmd.Parameters.Add("user_civilStatus", OleDbType.VarChar).Value = civilStatusBox.Text;
                cmd.Parameters.Add("user_birthday", OleDbType.Date).Value = DateTime.Parse(birthDateBox.Text);
                cmd.Parameters.Add("user_address", OleDbType.VarChar).Value = addressBox.Text;
                cmd.Parameters.Add("user_emailAddress", OleDbType.VarChar).Value = emailBox.Text;
                    
                    int i = cmd.ExecuteNonQuery();
                    if(i>0){
                        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);
            }

推荐答案

最好将连接字符串放在App.Config文件中
It will be better to put connection string in App.Config file
<appsettings>
    <connectionstrings>
        <add name="AccountingWindowApplication.Properties.Settings.WinPhoneAppConnectionString">
            connectionString="Data Source=COMPAQ-PC\SQLEXPRESS;Initial Catalog=WinPhoneApp;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </add></connectionstrings>  </appsettings>



并用作



and use as

SqlConnection cn = new SqlConnection();
       cn.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"].ToString();

         cn.Open();


希望能解决您的问题.
注意:将代码更改为Oledb


Hope it will solve your problem.
note:change code to Oledb


请参阅您的旧帖子......并查看评论...
祝你好运;
refer to you older post.......and see comments...
Best of luck;


这篇关于C#和msaccess中的INSERT语句问题...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆