문제
버튼 푸시에서 데이터를 데이터베이스에 저장하려고하지만 변수는 정의 된 위치의 특성에 따라 비공개로 보입니다. 나는 그들이 정의 된 곳으로 이동하려고 노력했지만 이것은 다른 오류를 일으키는 것 같습니다.
수정 사항이 주어지면 왜 그런 식으로 고정 되었습니까?
코드는 다음과 같습니다.
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 및 데이터 가능한 범위를 변경해야합니다. 생성자에 대해 선언하면 다른 방법으로는 accece를받을 수 없으므로 객체 인스턴스에 "글로벌"으로 객체 필드로 선언해야합니다.
각 변수가 필요한 범위를 찾아야합니다. 메소드 나 클래스 스코프 내부에서 메소드 외부로 선언 된 로컬 범위를 가질 수 있습니다.
어댑터는 클래스 자체가 아닌 Form1의 생성자로 범위를 지정합니다.
어댑터와 dtable을 클래스의 개인 구성원으로 옮깁니다.
업데이트 : [한숨] DTable을 클래스 COPE로 옮기는 것을 잊었습니다 ...
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가 어댑터와 마찬가지로했던 것처럼.
다른 오류가있을 수 있지만 컴파일러 오류를 게시하지 않았을 때 추측하기는 어렵습니다.
/johan/
제휴하지 않습니다 StackOverflow