帮助新接触C#变量的人

时间:2020-03-06 14:56:26  来源:igfitidea点击:

我试图通过按按钮将数据保存到数据库,但是由于变量的定义位置,这些变量似乎是私有的。我试图移动它们的定义位置,但这似乎会产生其他错误。

有了修复程序,为什么要这样修复呢?

代码如下。

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移至该班级。

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;
    }
}

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

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

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

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

adapter的范围是Form1的构造函数,而不是类本身。

将adapter和dtable移为该类的私有成员。

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范围更改为可用于按钮click方法事件。如果在构造函数上声明它们,则其他方法无法使用它们,则需要将它们声明为对象字段,以对对象实例"全局"。

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

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

可能还有其他错误,但是我们还没有猜测出编译器错误时,就很难猜测。

/约翰/