SQL错误-无错误消息-命令未执行

时间:2019-02-04 21:11:25

标签: c# sql ms-access oledb

我正在为我的公司开发用于资产管理的数据库应用程序。我没有使用SQL或MS Access进行数据库管理的大量经验。我正在研究通过在Visual Studio中使用C#通过运行SQL命令将数据添加到数据库中的解决方案。我在一些文本框中进行了工作,以揭示我的代码是否在不同的地方运行,我(我认为)将其范围缩小到了我的SQL,尽管我不确定,但我没有发现太多通过搜索了解Access和OleDb。

我更改了我的SQL代码的大写字母和措词以及逗号和引号,并且在可能被代码捕获错误的地方进行了修改。

private void SubmitButton_Click(object sender, EventArgs e)
        {
                try
                {
                    //declares connection
                    OleDbConnection con = new OleDbConnection();
                    OleDbCommand command = new OleDbCommand();
                    con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\eric.varner\Documents\AssetDB.accdb";
                    //open connection to Database
                    con.Open();
                    StatusLabel.Text = "Connected";
                    //declares command type
                    command.Connection = con;

                    //SQL  commands to call database to write data.

                    if (AssetTypeBox.Text == "iPad")
                    {
                        command.CommandText = "INSERT INTO AssetsiPad (Asset Tag, Condition, Location, Serial Number) VALUES('" + AssetBox.Text + "','" + ConditionBox.Text +
                            "','" + LocationBox.Text + "','" + SerialNumBox.Text + "')";
                    MessageBox.Show("The if statement runs fine");
                    }
                    else if (AssetTypeBox.Text == "iPhone")
                    {
                        command.CommandText = "INSERT INTO AssetsiPhone (Asset Tag, Condition, Location, Serial Number) VALUES('" + AssetBox.Text + "','" + ConditionBox.Text +
                            "','" + LocationBox.Text + "','" + SerialNumBox.Text + "')";
                    }
                    else if (AssetTypeBox.Text == "MR AP")
                    {
                        command.CommandText = "INSERT INTO AssetsMR (Asset Tag, Condition, Location, Serial Number, MAC Address, IP Address) VALUES('" + AssetBox.Text + "','" 
                        + ConditionBox.Text + "','" + LocationBox.Text + "','" + SerialNumBox.Text + "','" + MACaddressBox.Text + "','" + IPsubnetBox.Text + "')";
                    }
                    else if (AssetTypeBox.Text == "MX Security")
                    {
                        command.CommandText = "INSERT INTO AssetsMX (Asset Tag, Condition, Location, Serial Number) VALUES('" + AssetBox.Text + "','" + ConditionBox.Text + "','" 
                        + LocationBox.Text + "','" + SerialNumBox.Text + "',)";
                    }
                    else if (AssetTypeBox.Text == "Laptop")
                    {
                        command.CommandText = "INSERT INTO AssetsLaptop (Asset Tag, Condition, Location, Serial Number) VALUES('" + AssetBox.Text + "','" + ConditionBox.Text + "','" 
                        + LocationBox.Text + "','" + SerialNumBox.Text + "',)";
                    }
                    else
                    {
                        MessageBox.Show("you aren't reaching the correct statement");
                    }

                    command.ExecuteNonQuery();

                    //close connection to Database
                    con.Close();
                    MessageBox.Show("Data Saved");
                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "Not Connected";
                    MessageBox.Show("your sql didn't run correctly");
                }

        }

当我正确输入诸如“ iPad”之类的字符串时,我得到的消息框显示“ if语句运行正常”和“您的SQL未能正确运行”。 AssetTypeBox是唯一内置任何种类的捕获功能的东西。其他字段应该能够接受任何类型或数量的数据而不会出现问题。我希望我不会遗漏任何东西。

2 个答案:

答案 0 :(得分:2)

我认为INSERT命令不能接受带空格的字段名称,除非您将它们括在方括号中

[Asset Tag]

答案 1 :(得分:0)

The if statement runs fine 
Your SQL didn't run correctly

以上结果是可以预期的。当您运行command.ExecuteNonQuery();时,将执行SQL查询,这意味着在此之前您不会遇到异常。

带有 IPad check的if语句满足并且MessageBox.Show("The if statement runs fine");在代码执行command.ExecuteNonQuery();并发生异常之后运行。

由于您具有异常块,因此错误由下面的代码处理

 StatusLabel.Text = "Not Connected";
 MessageBox.Show("your sql didn't run correctly");