尝试并捕获错误“无法连接到数据库,请再试一次”

时间:2013-12-07 12:29:12

标签: 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.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 个答案:

答案 0 :(得分:2)

解决方案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)";