帮助不熟悉C#变量的人
我试图通过一个按钮将数据保存到数据库中,但是这些变量似乎是私有的,因为它们是在什么地方定义的。我试图将它们的定义转移到哪里,但这似乎会产生其他错误 如果有修复,为什么会这样修复 代码如下帮助不熟悉C#变量的人,c#,variables,C#,Variables,我试图通过一个按钮将数据保存到数据库中,但是这些变量似乎是私有的,因为它们是在什么地方定义的。我试图将它们的定义转移到哪里,但这似乎会产生其他错误 如果有修复,为什么会这样修复 代码如下 namespace enable { public partial class Form1 : Form { public Form1() { InitializeComponent(); OleDbConnecti
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;
}
}
您正在构造函数中声明
dTable
和adapter
,因此一旦构造函数完成,它就超出了范围
您希望将变量声明移出到主类中,如:
public partial class Form1 : Form
{
private DataTable dTable;
private OleDbDataAdapter adapter;
Public Form1()
{
... your setup here ...
dTable = new DataTable();
... etc ...
}
}
适配器的作用域是Form1的构造函数,而不是类本身 将适配器和数据表移动为类的私有成员
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范围更改为可访问button click方法事件。如果在构造函数中声明它们,则无法在其他方法中访问它们,则需要将它们声明为对象字段,以便对对象实例“全局”
您需要找出每个变量所需的作用域,您可以有一个局部作用域,即在方法内部声明或类作用域,在方法外部声明。适配器和dTable在构造函数中声明。它们都应该“移出”构造函数以获得类范围的独家新闻。就像弗朗西用适配器做的那样 可能还有其他错误,但很难猜测您何时没有发布编译器错误
/johan/名称空间块中有OleDbDataAdapter。是的,这就是我使用记事本编辑代码时发生的情况…:-)说明:基本上你是说“给我一个适配器对象,我将在这个方法中使用它(构造函数)”,这意味着当这个方法结束时,你不再需要它了。如果按照建议执行,您会说“我将在此类中使用适配器对象”。要查找的关键字是ScopeRetaged,因为我并不认为这(问题/解决方案)是关于MS Access的!:)