ConnectionString属性尚未初始化

时间:2014-01-21 22:04:53

标签: c#

收到运行时错误“ConnectionString属性尚未初始化”,当我“构建”项目时,它没有显示错误....

private void FrmPrincipal_Load(object sender, EventArgs e)
    {            
        button4.Enabled = false;
       this.ProductsTableAdapter.Fill(this.ProductDataSet.Products);
        this.SalesTableAdapter.Fill(this.SalesDataSet.Sales);
        string cnString = null;
        cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\POS.mdb";
        conn = new OleDbConnection(cnString);
        try
        {
            conn.Open();
        }
        catch (OleDbException ex)
        {
            MessageBox.Show(ex.Message, "Error..", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {

        }
        txtVAT.Text = "";
        txtCash.Text = "";
        txtChange.Text = "";
        txtTotal.Text = "";
        SalesIDTextBox.Text = "";

    }

1 个答案:

答案 0 :(得分:2)

我将根据您提供的少量信息猜测您在打开连接之前尝试填充适配器。尝试将该代码移动到try/catch

    string cnString = null;
    cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\POS.mdb";
    conn = new OleDbConnection(cnString);
    try
    {
        conn.Open();
        // Now that the connection is open, it can be used below
        this.ProductsTableAdapter.Fill(this.ProductDataSet.Products);
        this.SalesTableAdapter.Fill(this.SalesDataSet.Sales);
    }
    catch (OleDbException ex)
    {
        MessageBox.Show(ex.Message, "Error..", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }