无法插入数据库中的数据 [英] Unable to INSERT data in database

查看:83
本文介绍了无法插入数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于项目的Microsoft Access数据库文件.
这是我的规格:

I have a Microsoft Access Database file that I''m using for my project.
Here are my specs:

OS: Windows 7
IDE: Visual Studio 2010 Ultimate
Database Tool: Access 2007
Application Build Settings: x86 Debug



我的代码:



My Code:

using System.Data.OleDb;//Required
private void simpleButton1_Click(object sender, EventArgs e)
        {
//Here I check if the inputs are empty or null.
            if (string.IsNullOrEmpty(textEdit1.Text) == false || string.IsNullOrEmpty(textEdit2.Text) == false || string.IsNullOrEmpty(textEdit3.Text) == false)
            {
//Function made to insert data
                SaveData(textEdit1.Text, textEdit2.Text, textEdit3.Text);
//Post action display message
                MessageBox.Show("User creation successful!");
                this.Close();
            }
            else
            {
                MessageBox.Show("Please fill out all of the information");
            }
        }
        void SaveData(string userName, string passWord, string email)
        {
            try//not sure how to use try/catch here but I tried
            {
//I'm using the Jet db provider so I can use the .mdb db file instead of the newer file type
                string connectionString = @"Data Source=C:\Users\Sagitta\Documents\RS_Studio_Login.mdb";
                 // SQL command
                System.Data.OleDb.OleDbConnection con = new OleDbConnection(connectionString);
                con.Open();
                string sqlCommand = "INSERT INTO Users (Username,Password,RegisterEmail)  " + "VALUES('" + userName + "','" + passWord + "','" + email + "')";
                System.Data.OleDb.OleDbCommand cmd = new OleDbCommand(sqlCommand,con);
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }



我的代码说明:

当用户想要创建一个新的登录ID时,将出现一个新表格.新表格将具有3个文本框.用户名的第一个文本框,密码的第二个文本框,电子邮件地址的第三个文本框(用于恢复登录信息).
数据库设置如下:
表:用户
列1:ID
列2:用户名
第3栏:密码
列4:RegisterEmail

问题:

我无法获取将数据插入数据库的代码.我运行它并尝试使用尝试插入数据库的数据登录.我也尝试在Microsoft Access中打开文件以查看文件是否有更改.



My Code Explained:

When the user wants to create a new login id, a new form will appear. The new form will have 3 textboxes. First textbox for username, second for password and third for email address (to recover login info).
The database is setup as follows:
Table: Users
Column1: ID
Column2: Username
Column3: Password
Column4: RegisterEmail

Problem:

I can''t get my code to insert the data into the database. I ran it and tried to login with the data I tried to insert into the database. Also I tried opening the file in Microsoft Access to see if there were changes to the file.

推荐答案

它可能对您有所帮助

it may help u

string sqlCommand = "INSERT INTO Users (Username,[Password],RegisterEmail) VALUES ('" + userName + "','" + passWord + "','" + email + "')";



这是标准查询格式



this is the standard query format


在您检查连接字符串时会发生此异常.
this exception occur in you connection string check it .


尝试

try

string sqlCommand = "INSERT INTO Users (Username,[Password],RegisterEmail)  " + "VALUES('" + userName + "','" + passWord + "','" + email + "')";



并使用提供程序的连接字符串



And use connection string with Provider

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Users\Sagitta\Documents\RS_Studio_Login.mdb";



有关连接字符串的更多信息:
http://www.connectionstrings.com/access/ [



More information about connection strings :
http://www.connectionstrings.com/access/[^]


这篇关于无法插入数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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