连接到Access数据库时,vshost32.exe停止工作

时间:2016-03-12 05:27:17

标签: c# visual-studio-2012 oledb ms-access-2016

我正在使用Visual Studio 2012并将其连接到MS Access 2016.我的系统正常运行但没有任何错误,但当我尝试单击搜索按钮时,vshost32.exe始终停止工作。

这是我的代码:

namespace WindowsFormsApplication4
{
public partial class SalesInventory : Form
{

    public SalesInventory()
    {
        InitializeComponent();
    }

    private void SearchButton_Click(object sender, EventArgs e)
    {
        string source = @"Provider= Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Irma\\Documents\\project.accbd;Persist Securit Info= False";
        OleDbConnection conn = new OleDbConnection(source);
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
            string str = "Select * FROM Products WHERE Product_Name LIKE '%" + SearchTextBox.Text + "%'";
            OleDbDataAdapter da = new OleDbDataAdapter(str, conn);

            DataTable ds = new DataTable();
            ds.Clear();
            da.Fill(ds);
            GrindView.DataSource = ds;
            conn.Close();

        }
        else
        {
            string str = "Select * FROM Products WHERE Product_Name LIKE '%" + SearchTextBox.Text + "%'";
            OleDbDataAdapter da = new OleDbDataAdapter(str, conn);

            DataTable ds = new DataTable();
            ds.Clear();
            da.Fill(ds);
            GrindView.DataSource = ds;
            conn.Close();

        }
    }
}
}

即使使用此代码,vshost32.exe也会停止工作:

    private void AddBut_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider= Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Irma\\Documents\\project.accbd;Persist Security Info= False";

        OleDbCommand command = new OleDbCommand("Insert into Product([Product_Name]) VALUES (@ProdName)");
        command.Connection = conn;
        conn.Open();
        if (conn.State == ConnectionState.Open)
        {
            command.Parameters.AddWithValue("@ProdName", ProdNameText.Text);


            try
            {
                command.ExecuteNonQuery();
                MessageBox.Show("Data Added");
                conn.Close();
                OleDbCommand cmd = new OleDbCommand("Insert into product_Fields([Product_Name],[Description],[Category],[Quantity],[Price],[Supplier_Name]) VALUES (@prodName,@Description,@Category,@Quantity,@Price,@Supplier_Name)");
                cmd.Connection = conn;
                conn.Open();
                if (conn.State == ConnectionState.Open)
                {

                    cmd.Parameters.AddWithValue("@prodName", ProdNameText.Text);
                    cmd.Parameters.AddWithValue("@Description", DescriptionComboBox.Text);
                    cmd.Parameters.AddWithValue("@Category", CategoryComboBox.Text);
                    cmd.Parameters.AddWithValue("@Quantity", QuantityBox.Text);
                    cmd.Parameters.AddWithValue("@Price", PriceBox.Text);
                    cmd.Parameters.AddWithValue("@Supplier_Name", SupplierNameText.Text);
                }
                try
                {
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Data Added");
                    conn.Close();

                }
                catch
                {
                    MessageBox.Show("Error");
                    conn.Close();
                }
                }

            catch
            {

                MessageBox.Show("Error");
                conn.Close();

            }
        }
        else 
        {
            MessageBox.Show("Data Connection Failed");
            this.Close();
        }

        }

0 个答案:

没有答案