简体   繁体   English

试图从C#将数据插入到Access数据库中,但是失败了,我也不知道为什么

[英]Trying to insert data into an access database from C#, but it fails, and I have no idea why

After browsing this site for some time, I have been able to make a start on a piece of code that adds information from a set of text boxes into a pre-set database. 浏览该站点一段时间后,我就可以从一段代码开始,该代码将来自一组文本框的信息添加到预设数据库中。

Here is that code: 这是该代码:

string connString = "Provider=Microsoft.ACE.OLEDB.12.0;data source=[[Rather not release private information, but trust me, this part works]]";

var _ID = LblID.Text;
var _FirstName = TxtUsername.Text;
var _LastName = TxtPassword.Text;
var _DOB = TxtFirstName.Text;
var _Number1 = TxtLastName.Text;
var _Number2 = TxtDOB.Text;
var _Email = TxtNumber2.Text;
var _Username = TxtEmail.Text;
var _Password = TxtNumber1.Text;

using (OleDbConnection conn = new OleDbConnection(connString))
{

    OleDbCommand cmd = new OleDbCommand("INSERT into Users ([_ID], [_FirstName], [_LastName], [_DOB], [_Number1], [_Number2], [_Email], [_Username], [_Password]) Values(@ID Number, @First Name, @Last Name, @Date of Birth, @Phone Number 1, @Phone Number 2, @Email, @Username, @Password)");

    cmd.Connection = conn;

    conn.Open();

    if (conn.State == ConnectionState.Open)
    {
        cmd.Parameters.Add("@ID Number", OleDbType.VarChar).Value = _ID;
        cmd.Parameters.Add("@First Name", OleDbType.VarChar).Value = _FirstName;
        cmd.Parameters.Add("@Last Name", OleDbType.VarChar).Value = _LastName;
        cmd.Parameters.Add("@Date of Birth", OleDbType.VarChar).Value = _DOB;
        cmd.Parameters.Add("@Phone Number 1", OleDbType.VarChar).Value = _Number1;
        cmd.Parameters.Add("@Phone Number 2", OleDbType.VarChar).Value = _Number2;
        cmd.Parameters.Add("@Email", OleDbType.VarChar).Value = _Email;
        cmd.Parameters.Add("@Username", OleDbType.VarChar).Value = _Username;
        cmd.Parameters.Add("@Password", OleDbType.VarChar).Value = _Password;

        try
        {
            cmd.ExecuteNonQuery();
            MessageBox.Show("Data Added");
            conn.Close();
        }
        catch (OleDbException ex)
        {
            MessageBox.Show(ex.Message);
            conn.Close();
        }
    }
    else
    {
        MessageBox.Show("Connection Failed");
    }
}

Its a bit long, but stay with me here. 它有点长,但请留在我这里。

This code is very similar to one I have found from an answer to a previous question , of course, renaming variables and such to integrate the code into my application. 此代码是非常相似的一个我从一个答案发现一个先前的问题 ,当然,重命名变量,这样的代码集成到我的应用程序。

However, after doing a step by step breakdown of how the application is processing this, it seems to fail here: 但是,在逐步分析应用程序的处理方式之后,似乎在这里失败了:

try
{
    cmd.ExecuteNonQuery();
    MessageBox.Show("Data Added");
    conn.Close();
}

More specifically, the cmd.ExecuteNonQuery(). 更具体地说,是cmd.ExecuteNonQuery()。

It seems that the try stops at this part, and skips straight to the catch. 似乎尝试在这一部分停止,然后直接跳到陷阱。

My question is, what am I doing wrong? 我的问题是,我在做什么错? How badly have I fuc- messed this up? 我搞砸了多少?

If you need any specifics, just comment and I'll respond as soon as I can. 如果您需要任何具体说明,请发表评论,我会尽快回复。

Thanks in advance. 提前致谢。

Edit 1: Forgot to add the error message, my apologies. 编辑1:抱歉,添加错误消息,对不起。

"Syntax error (missing operator) in query expression '@ID Number'." “查询表达式“ @ID号”中的语法错误(缺少运算符)。”

Edit 2: Not sure if this should be an edit, but now I'm getting an entirely new problem. 编辑2:不确定是否应该进行编辑,但是现在我遇到了一个全新的问题。

After reading the comments, I have changed the area names (both in the application and in the database), and now I'm getting an entirely new error message. 阅读注释后,我更改了区域名称(在应用程序和数据库中),现在我得到了一条全新的错误消息。

"The INSERT INTO statement contains the following unknown field name: '_ID'." “ INSERT INTO语句包含以下未知字段名称:'_ ID'。”

I have a feeling it will complain the same for the rest of them. 我有一种感觉,其他人也会抱怨。

Sorry about all of the trouble. 对不起,所有的麻烦。

Spaces are not allowed in Access SQL without backquotes around them. 在Access SQL中不允许使用空格,且空格之间不能包含反引号。 Since parameter names are not tied to anything in the database, simply remove the spaces from them: 由于参数名称不与数据库中的任何内容绑定,因此只需删除它们中的空格即可:

... VALUES (@IDNumber, @FirstName, @LastName, ...)

cmd.Parameters.Add("@IDNumber", OleDbType.VarChar).Value = _ID;
cmd.Parameters.Add("@FirstName", OleDbType.VarChar).Value = _FirstName;
cmd.Parameters.Add("@LastName", OleDbType.VarChar).Value = _LastName;

"The INSERT INTO statement contains the following unknown field name: '_ID'." “ INSERT INTO语句包含以下未知字段名称:'_ ID'。”

This happens because _ID does not match the name of the column in your database. 发生这种情况是因为_ID与数据库中的列名不匹配。 Unlike parameter names of which you have complete control, column names are part of the database schema, so you need to use the exact name, including any spaces that it may have. 与可以完全控制的参数名称不同,列名称是数据库架构的一部分,因此您需要使用确切的名称,包括其可能包含的任何空格。 Use square brackets for names with spaces: 对于带有空格的名称,请使用方括号:

INSERT INTO Users ([ID Number], [First Name], ... ) ...

As the exception tells you, you are missing a comma in your VALUES list right after the @ID parameter. 异常告诉您,在@ID参数后面的VALUES列表中缺少逗号。

For your second error-message: do you have a column in your database-table that is called _ID ? 对于第二个错误消息:数据库表中是否有一个名为_ID的列?

Next to that, you do not have to close the connection explicitly, since you're already using a using statement for the db-connection. 紧接着,您不必显式关闭连接,因为您已经对db-connection使用了using语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从C#插入Access数据库时数据类型不匹配 - data type mismatch when trying to insert into an Access database from C# 无法从Visual C#将数据插入MS Access数据库 - Unable to insert data into MS Access database from Visual C# 尝试使用 C# 将数据插入 MS Access 数据库,但插入命令不起作用 - Trying to insert data into MS Access database using C# but insert command is not working 我如何从C#中的Sqlserver 2008数据库表将数据插入MS Access - how can i insert data into MS Access from Sqlserver 2008 database table in C# 错误-将数据插入C#中的Access数据库 - Error - Insert data into Access database in c# 插入数据以使用C#访问数据库 - Insert data in to access database with C# 我已经设置了Web服务,但不知道如何在MVC 5 C#中从中获取数据 - I have set up a web service but have no idea how to get data from it in MVC 5 c# C# INSERT INTO Access 数据库失败,没有给出错误 - C# INSERT INTO Access database fails, no errors given 从C#批量插入Access数据库? - Bulk Insert into access database from c#? 将数据从DataGridView插入到C#中的Access数据库中(但是它能正常工作,在insert into语句中出现语法错误) - Insert data from DataGridView to Access Database in C# (however it works ,syntax error in insert into statement appear)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM