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

本文关键字:初始化 属性 连接 错误 | 更新日期: 2023-09-27 17:49:40

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."

怎么了?

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

我不是专家,但不妨试试

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

你可以检查你的ConnectionString是否有效:http://www.connectionstrings.com/

请注意,提供程序是不同的,关于您正在使用的.mdb版本(2007,2000…)

顺便说一下,你想试试Source=D:'MyWorkers1。mdb而不是Source=D:/MyWorkers1 ?用"'"代替"/"

我在同一个例子上工作,并认为con.Dispose();是在表单加载不允许da.Update(ds1, "MyWorkers1");更新记录。所以目前我已经删除了它,但它最好是移动到销毁/卸载等(我真的不知道哪个是正确的事件关闭表单之前)

异常非常明显。ConnectionString属性没有"正确"设置,当试图调用Open SqlConnection对象时。

你试过在web.config中创建一个吗?