2008-09-30 38 views
3

我試圖在按鈕上將數據保存到數據庫,但這些變量似乎是由它們定義位置的性質來私有的。我試圖移動他們定義的位置,但這似乎產生了其他錯誤。幫助新來的C#變量的人

給定一個修復程序,爲什麼修復這種方式?

代碼如下。

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. 
     } 
    } 
} 
+0

因爲我真的不認爲這(問題/解決方案)是關於MS Access! :) – 2008-09-30 19:27:10

回答

10

你聲明在構造函數中dTableadapter,所以它超出範圍只要構造函數完成。

您想要移動的變量聲明伸到主類,如:

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

    Public Form1() 
    { 
     ... your setup here ... 
     dTable = new DataTable(); 
     ... etc ... 
    } 
} 
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; 
    } 
} 
+0

您錯過了數據表 – albertein 2008-09-30 19:01:43

+0

您的名字空間塊中有OleDbDataAdapter。 – FlySwat 2008-09-30 19:01:50

1

適配器的作用域到Form1的構造函數,而不是類本身。

將適配器和dtable移動到該類的私有成員。

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範圍更改爲可點擊按鈕單擊方法事件。如果您在構造函數中聲明它們,則無法在其他方法上使用它們,您需要將它們聲明爲對象字段以便對對象實例「全局」。

您需要找出哪個範圍需要每個變量,您可以有一個局部範圍,即在一個方法或類範圍內聲明,在方法外部聲明。

-1

適配器和dTable在您的構造函數中聲明。他們都應該被移出構造函數以獲得類廣泛的獨家新聞。就像Franci對適配器所做的一樣。

可能還有其他錯誤,但很難猜出何時未發佈編譯器錯誤。

/johan/