2

我试图在按钮按下时将数据保存到数据库中,但这些变量似乎是私有的,因为它们被定义的性质。我试图移动它们定义的位置,但这似乎会产生其他错误。

给定一个修复程序,为什么它是这样修复的?

代码如下。

namespace enable
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            OleDbConnection favouriteConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\192.168.123.5\\Share\\Matt\\BugTypes.mdb");
            string strSQL = "SELECT CategoryName, Show " + "FROM [Categories] WHERE Show = 'Yes' " + "ORDER BY CategoryName";
            OleDbDataAdapter adapter = new OleDbDataAdapter(strSQL, favouriteConnection);
            OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(adapter);
            DataTable dTable = new DataTable();
            adapter.Fill(dTable);
            BindingSource bSource = new BindingSource();
            bSource.DataSource = dTable;
            dataGridView1.DataSource = bSource;
            adapter.Update(dTable);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            adapter.Update(dTable);//this is the button that needs to do the save, but can't see the variables.
        }
    }
}
4

5 回答 5

10

您在构造函数中声明dTableadapter,因此一旦构造函数完成,它就会超出范围。

您想将变量声明移到主类中,例如:

public partial class Form1 : Form
{
    private DataTable dTable;
    private OleDbDataAdapter adapter;

    Public Form1()
    {
         ... your setup here ...
         dTable = new DataTable();
         ... etc ...
    }
}
于 2008-09-30T19:01:04.073 回答
3
namespace enable
{    
    public partial class Form1 : Form
    {

    OleDbDataAdapter adapter;
    DataTable dTable = new DataTable();

        public Form1()
        {
            InitializeComponent();
            OleDbConnection favouriteConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\192.168.123.5\\Share\\Matt\\BugTypes.mdb");
            string strSQL = "SELECT CategoryName, Show " + "FROM [Categories] WHERE Show = 'Yes' " + "ORDER BY CategoryName";
            adapter = new OleDbDataAdapter(strSQL, favouriteConnection);
            OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(adapter);
            adapter.Fill(dTable);
            BindingSource bSource = new BindingSource();
            bSource.DataSource = dTable;
            dataGridView1.DataSource = bSource;
            adapter.Update(dTable);            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            adapter.Update(dTable);//this is the button that needs to do the save, but can't see the variables.
        }
    }
}

您需要更改 DataAdapter 和 dataTable 范围才能访问按钮单击方法事件。如果您在构造函数上声明它们,则它们不能在其他方法上使用,您需要将它们声明为对象字段,以便对您的对象实例是“全局的”。

你需要找出每个变量需要什么作用域,你可以有一个局部作用域,即声明在方法内或类作用域,声明在方法外。

于 2008-09-30T19:01:15.880 回答
1

适配器的作用域是 Form1 的构造函数,而不是类本身。

将适配器和 dtable 移动为类的私有成员。

于 2008-09-30T19:01:09.347 回答
0

更新:[叹气]我也忘了把 dTable 移到班级应付......

namespace enable
{    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            OleDbConnection favouriteConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\192.168.123.5\\Share\\Matt\\BugTypes.mdb");
            string strSQL = "SELECT CategoryName, Show " + "FROM [Categories] WHERE Show = 'Yes' " + "ORDER BY CategoryName";
            m_Adapter = new OleDbDataAdapter(strSQL, favouriteConnection)l
            OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(m_Adapter);
            dTable = new DataTable();
            m_Adapter.Fill(dTable);
            BindingSource bSource = new BindingSource();
            bSource.DataSource = dTable;
            dataGridView1.DataSource = bSource;
            m_Adapter.Update(dTable);            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_Adapter.Update(dTable);//this is the button that needs to do the save, but can't see the variables.
        }

        OleDbDataAdapter m_Adapter;
        DataTable dTable;
    }
}
于 2008-09-30T19:00:13.670 回答
-1

适配器和 dTable 在您的构造函数中声明。它们都应该从构造函数中“移出”以获得类范围的独家新闻。就像 Franci 对适配器所做的那样。

可能还有其他错误,但是当您没有发布编译器错误时很难猜测。

/约翰/

于 2008-09-30T19:06:10.410 回答