Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “尝试捕捉错误”;无法连接到数据库,请重试“;_C#_Visual Studio 2012 - Fatal编程技术网

C# “尝试捕捉错误”;无法连接到数据库,请重试“;

C# “尝试捕捉错误”;无法连接到数据库,请重试“;,c#,visual-studio-2012,C#,Visual Studio 2012,当我尝试将数据提交到表单中,以便将其插入数据库时,会收到一条错误消息,提示: “无法连接到数据库,请重试” 我认为这与我的catch语句有关,下面是代码: ``using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.T

当我尝试将数据提交到表单中,以便将其插入数据库时,会收到一条错误消息,提示:

“无法连接到数据库,请重试”

我认为这与我的catch语句有关,下面是代码:

``using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace EuroNetbankCoursework
{
public partial class CustomerDetailsForm : Form
{
    public CustomerDetailsForm()
    {
        InitializeComponent();
    }
    string EmployeeConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=""\\na1b\uj\UP647529\Year 2\Managing Data and Security\Employee.accdb""";
    private void CustomerDetailsForm_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'employeeDataSet.Customer' table. You can move, or remove it, as needed.
        this.customerTableAdapter.Fill(this.employeeDataSet.Customer);

    }

    private void Submit_Click(object sender, EventArgs e)
    {
        OleDbConnection myConn = new OleDbConnection(EmployeeConnectionString);
        OleDbCommand SQLinsert = myConn.CreateCommand();
        SQLinsert.CommandText = @"INSERT INTO Customer (Customer ID, Username, Password, First Name, Surname, Street Name, Town, County, Post Code, Telephone Number) VALUES (?,?,?,?,?,?,?,?,?,?)";
        SQLinsert.Parameters.AddWithValue("CustomerID", textBoxCID.Text);
        SQLinsert.Parameters.AddWithValue("Username", textBoxUN.Text);
        SQLinsert.Parameters.AddWithValue("Password", textBoxPW.Text);
        SQLinsert.Parameters.AddWithValue("FirstName", textBoxFN.Text);
        SQLinsert.Parameters.AddWithValue("Surname", textBoxS.Text);
        SQLinsert.Parameters.AddWithValue("StreetName", textBoxSN.Text);
        SQLinsert.Parameters.AddWithValue("Town", textBoxT.Text);
        SQLinsert.Parameters.AddWithValue("County", textBoxCt.Text);
        SQLinsert.Parameters.AddWithValue("Postcode", textBoxPoc.Text);
        SQLinsert.Parameters.AddWithValue("TelephoneNumber", textBoxTN.Text);
        try
        {
            myConn.Open();
            SQLinsert.ExecuteNonQuery();
            myConn.Close();

            MessageBox.Show("You have successfully created a new incident record for the Case " + textBoxCID.Text, textBoxUN.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            Close();

        }

        catch (ConstraintException)
        {
            MessageBox.Show("An error occured when submitting a new incident record", " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        catch (OleDbException)
        {
            MessageBox.Show("Unable to connect to the DB.  Please try again", " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        catch (NoNullAllowedException)
        {
            MessageBox.Show("Please ensure that you have entered a value for the Case Reference and the Incident Reference", " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch (InvalidOperationException)
        {
            MessageBox.Show("A previous connection to the DB was not closed.", " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void customerBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.customerBindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.employeeDataSet);

    }
}
}


有人知道我尝试输入数据时错误不断出现的原因吗?

解决方案1:您的查询字符串无效

试试这个:

string EmployeeConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\na1b\uj\UP647529\Year 2\Managing Data and Security\Employee.accdb";
解决方案2:您的参数需要更改:

SQLinsert.CommandText = @"INSERT INTO Customer ([Customer ID], Username, Password, First Name, Surname, [Street Name], Town, County, [Post Code], [Telephone Number]) VALUES (@CustomerID,@Username,@Password,@FirstName,@Surname,@StreetName,@Town,@City,@Postcode,@TelephoneNumber)";

好。。。您的代码以某种方式抛出了一个
OLEDBEException
,该异常将被相应的
catch
捕获。您可以尝试在此处设置断点,以查看导致异常的原因。我尝试过这样做,但仍然无效:/n您知道其他解决方案吗?Thanks@user3041656:检查我编辑的答案,查看章节
解决方案2
您好,我修复了参数名称并在前面添加了@,但仍然出现错误“无法连接到数据库请重试”有人知道问题吗?@user3041656:请告诉我是
na1b
机器名还是文件夹名?