2

这是我的简单代码,只是为了从 MySQL 中读取一些内容。但我想要的是在打开表单时创建连接和命令,并在单击按钮时打开连接并执行其余操作。但它说:

“当前上下文中不存在名称‘konekcija’”

有人可以解释一下吗。

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();
        }
    }
}
4

3 回答 3

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();
    //...
}
于 2012-11-05T23:18:58.860 回答
0

您应该MySqlConnection在 EventHandler 之外声明,Form_Load以便您可以从其他方法访问它。另外我建议在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();
    }
}

}

于 2012-11-05T23:33:28.417 回答
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());
                }
            }
        }
    }
}
于 2012-11-06T03:50:36.597 回答