错误:连接属性尚未初始化

时间:2011-04-08 04:40:40

标签: c# asp.net database

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        System.Data.OleDb.OleDbConnection con;
        DataSet ds1;
        System.Data.OleDb.OleDbDataAdapter da;

        int MaxRows = 0;
        int inc = 0;

        private void Form1_Load(object sender, EventArgs e)
        {
            con = new System.Data.OleDb.OleDbConnection();
            ds1 = new DataSet();

            con.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/MyWorkers1.mdb";

            string sql = "SELECT * from tblWorkers";
            da = new System.Data.OleDb.OleDbDataAdapter(sql, con);

            con.Open();
            da.Fill(ds1, "MyWorkers1");
            NavigateRecords();
            MaxRows = ds1.Tables["MyWorkers1"].Rows.Count;
            //MaxRows = ds1.Tables["MyWorkers1"].Rows[inc];
            //MessageBox.Show("Database open");

            con.Close();
            //MessageBox.Show("Database close");

            con.Dispose();
        }

        private void NavigateRecords()
        {
            DataRow drow = ds1.Tables["MyWorkers1"].Rows[inc];
            textBox1.Text = drow.ItemArray.GetValue(0).ToString();
            textBox2.Text = drow.ItemArray.GetValue(1).ToString();
            textBox3.Text = drow.ItemArray.GetValue(2).ToString();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (inc != MaxRows - 1)
            {
                inc++;
                NavigateRecords();
            }
            else
            {
                MessageBox.Show("No More Records");
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (inc > 0)
            {
                inc--;
                NavigateRecords();
            }
            else
            {
                MessageBox.Show("First Record");
            }
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {

            if (inc != 0)
            {
                inc = 0;
                NavigateRecords();
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (inc != MaxRows - 1)
            {
                inc = MaxRows - 1;
                NavigateRecords();
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbCommandBuilder cb;
            cb = new System.Data.OleDb.OleDbCommandBuilder(da);

            DataRow drow = ds1.Tables["MyWorkers1"].NewRow();

            drow[0] = textBox1.Text;
            drow[1] = textBox2.Text;
            drow[2] = textBox3.Text;

            ds1.Tables["MyWorkers1"].Rows.Add(drow);

            MaxRows = MaxRows + 1;
            inc = MaxRows - 1;

            da.Update(ds1, "MyWorkers1");

            MessageBox.Show("Record / Entry Added");
        }       

    }
}

当我运行它时会显示"Invalid Operation - Exception Unhandled connection property has not been initialized."

之类的错误

有什么问题?

4 个答案:

答案 0 :(得分:1)

我不是专家,但请尝试

con = new System.Data.OleDb.OleDbConnection(you_connection_string);

答案 1 :(得分:1)

您可以在此处检查您的ConnectionString是否有效:http://www.connectionstrings.com/

请注意,就您正在使用的.mdb版本(2007,2000 ...)

而言,提供商是不同的

顺便问一下,你想尝试Source = D:\ MyWorkers1.mdb而不是Source = D:/ MyWorkers1吗?使用“\”而不是“/".

答案 2 :(得分:1)

我正在研究相同的示例,并认为表单加载中的con.Dispose();不允许da.Update(ds1, "MyWorkers1");更新记录。所以暂时我已将其删除,但最好将其移至Destroy / Unload等(我真的不知道在关闭表单之前哪个是正确的事件)

答案 3 :(得分:0)

异常非常清楚。 ConnectionString属性尚未“正确”设置, 尝试调用时打开SqlConnection对象。

您是否尝试过在web.config中创建一个?