2012-11-05 75 views
2

這是我簡單的代碼,只是爲了從MySQL讀取一些東西。但是我想要的是在Form打開時創建連接和命令,只需點擊按鈕時打開連接,然後執行其他操作。但它說將C#連接到MySQL並打開連接

"The name 'konekcija' does not exist in the current context" 

有人可以幫助我,請解釋我。

namespace mysql_windows_console 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     public void Form1_Load(object sender, EventArgs e) 
     { 
      /*========MYSQL KONEKCIJA===========*/ 
      string baza    = "server=localhost;database=test;user=root;password=;"; 
      MySqlConnection konekcija = new MySqlConnection(baza); 
      MySqlCommand comm   = konekcija.CreateCommand(); 
      /*========MYSQL KONEKCIJA===========*/ 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      konekcija.Open(); 
      string sql    = "SELECT IME,PREZIME FROM tabela"; 
      MySqlDataAdapter adapter = new MySqlDataAdapter(sql,konekcija); 
      DataTable tab   = new DataTable(); 
      adapter.Fill(tab); 
      dataGridView1.DataSource = tab; 
      konekcija.Close(); 
     } 
    } 
} 
+1

其範圍問題,在Form1_Load中創建的MySQLConnection對象konekcija在該方法中是本地的,並且在button1_Click中不可訪問。你需要聲明它是類本身的一個屬性... – WebChemist

+0

不錯的變量名稱:) – nawfal

+0

@ NikolaD-Nick我沒有嘲笑或取笑。我發現它很好,所有:) – nawfal

回答

0

你應該申報MySqlConnection外事件處理程序,讓您可以從其他的方法來訪問它。另外我會建議在Form構造函數中初始化它。由於您使用的是DataAdapter,因此不需要使用MySqlCommand。這是修改後的代碼;

namespace mysql_windows_console 
{ 
public partial class Form1 : Form 
{ 
    MySqlConnection konekcija; 
    string baza = "server=localhost;database=test;user=root;password=;"; //so you can access it again if you need it b any chance 
    public Form1() 
    { 
     InitializeComponent(); 
     konekcija = new MySqlConnection(baza); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     konekcija.Open(); 
     string sql = "SELECT IME,PREZIME FROM tabela"; 
     MySqlDataAdapter adapter = new MySqlDataAdapter(sql,konekcija); 
     DataTable tab = new DataTable(); 
     adapter.Fill(tab); 
     dataGridView1.DataSource = tab; 
     konekcija.Close(); 
    } 
} 

}

0

您正在將konekcija存儲爲局部變量。使它成爲一個屬性,如:

private MySqlConnection konekcija { get; set; } 

public void Form1_Load(object sender, EventArgs e) 
{ 
    //... 
    this.konekcija = new MySqlConnection(baza); 
} 

private void button1_click(object sender, EventArgs e) 
{ 
    this.konekcija.Open(); 
    //... 
} 
0

它只是意味着konekcija不上的button1_Click範圍找到。

儘量減少變量的範圍。爲什麼不在需要時連接?例如,

const string baza = "server=localhost;database=test;user=root;password=;"; 
private void button1_Click(object sender, EventArgs e) 
{ 

    using (MySqlConnection _conn = new MySqlConnection(baza)) 
    { 
     using (MySqlCommand _comm = new MySqlCommand()) 
     { 
      _comm.Connection = _conn; 
      _comm.CommandText = "SELECT IME,PREZIME FROM tabela"; 
      _comm.CommandType = CommandType.Text; 

      using (MySqlDataAdapter _adapter = new MySqlDataAdapter(_comm)) 
      { 
       DataTable _table = new DataTable; 
       try 
       { 
        _adapter.Fill(_table); 
        dataGridView1.DataSource = _table; 
       } 
       catch (MySqlException e) 
       { 
        MessageBox.Show(e.Message.ToString()); 
       } 
      } 
     } 
    } 
}