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

给定修复,为什么这样修复?

代码如下。

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.
        }
    }
}
有帮助吗?

解决方案

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

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

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

    Public Form1()
    {
         ... your setup here ...
         dTable = new DataTable();
         ... etc ...
    }
}

其他提示

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范围更改为可以访问按钮单击方法事件。如果在构造函数上声明它们,则无法在其他方法上对它们进行声明,则需要将它们声明为“全局”对象字段。到你的对象实例。

您需要找出每个变量需要的范围,您可以拥有一个局部范围,即在方法或类范围内声明,在方法外部声明。

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

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

更新:[叹气]我忘了将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;
    }
}

适配器和dTable在构造函数中声明。他们都应该被“移出”构造函数以获得类广泛的独家新闻。正如Franci对适配器所做的那样。

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

/约翰/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top