C#Mysql Link Combobox与文本框中的数据库值

时间:2014-11-25 15:04:33

标签: c# mysql combobox database-connection

我有一个包含ComboBox的表单,该表单必须显示名称,选择名称时必须在不同的文本框中显示我的mysql数据库中的不同值。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Dark_Heresy
{
    public partial class Required_Attributes : Form
    {
        public Required_Attributes()
        {
            InitializeComponent();
        }

        private void cb_Talents_SelectedIndexChanged(object sender, EventArgs e)
        {
            string constring = "datasource = localhost; port = 3306; username = root; password = Mypass;";
            string Query = "SELECT * FROM dark_heresy.talents WHERE TalentName='" + cb_Talents.Text + "' ;";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;

            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();

                while (myReader.Read())
                {

                    string description = myReader.GetString("Description");
                    string strength = myReader.GetInt32("R_Str").ToString();
                    string weaponskill = myReader.GetInt32("R_WS").ToString();
                    string ballisticskill = myReader.GetInt32("R_BS").ToString();
                    string fellowship = myReader.GetInt32("R_Fel").ToString();
                    string perception = myReader.GetInt32("R_Per").ToString();
                    string intelligence = myReader.GetInt32("R_Int").ToString();
                    string agility = myReader.GetInt32("R_Agi").ToString();
                    string willpower = myReader.GetInt32("R_WP").ToString();
                    string toughness = myReader.GetInt32("R_Tough").ToString();
                    string talentrequired = myReader.GetString("Talent_required");
                    string skillrequired = myReader.GetString("Skill_required");
                    string classrequired = myReader.GetString("Class_required");

                    TextDescription.Text = description;
                    TextStrengh.Text = strength;
                    TextWeaponskill.Text = weaponskill;
                    TextBallisticskill.Text = ballisticskill;
                    TextFellowship.Text = fellowship;
                    TextPerception.Text = perception;
                    TextIntelligence.Text = intelligence;
                    TextAgility.Text = agility;
                    TextWillpower.Text = willpower;
                    TextToughness.Text = toughness;
                    TextTalent.Text = talentrequired;
                    TextSkill.Text = skillrequired;
                    TextClass.Text = classrequired;



                }
            }

                catch (Exception ex)
                {
                    MessageBox.Show("Error: \r\n" + ex);
                }

            }
        }
    }

然而,当我打开表单并按下ComboBox时,没有任何反应,没有显示任何内容,没有语法错误,代码有什么问题?

即使我将代码更改为:

也会更新
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Dark_Heresy
{
    public partial class Talents : Form
    {
        public Talents()
        {
            InitializeComponent();
        }

        private void cb_Talents_SelectedIndexChanged(object sender, EventArgs e)
        {

            MessageBox.Show("Test");
            //string constring = "datasource = localhost; port = 3306; username = root; password = Lorena89;";
            //string Query = "SELECT * FROM dark_heresy.talents WHERE TalentName='" + cb_Talents.Text + "' ;";
            //MySqlConnection conDataBase = new MySqlConnection(constring);
            //MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            //MySqlDataReader myReader;

            //try
            //{
            //    conDataBase.Open();
            //    myReader = cmdDataBase.ExecuteReader();

            //    while (myReader.Read())
            //    {

            //        string description = myReader.GetString("Description");
            //        string strength = myReader.GetInt32("R_Str").ToString();
            //        string weaponskill = myReader.GetInt32("R_WS").ToString();
            //        string ballisticskill = myReader.GetInt32("R_BS").ToString();
            //        string fellowship = myReader.GetInt32("R_Fel").ToString();
            //        string perception = myReader.GetInt32("R_Per").ToString();
            //        string intelligence = myReader.GetInt32("R_Int").ToString();
            //        string agility = myReader.GetInt32("R_Agi").ToString();
            //        string willpower = myReader.GetInt32("R_WP").ToString();
            //        string toughness = myReader.GetInt32("R_Tough").ToString();
            //        string talentrequired = myReader.GetString("Talent_required");
            //        string skillrequired = myReader.GetString("Skill_required");
            //        string classrequired = myReader.GetString("Class_required");

            //        TextDescription.Text = description;
            //        TextStrengh.Text = strength;
            //        TextWeaponskill.Text = weaponskill;
            //        TextBallisticskill.Text = ballisticskill;
            //        TextFellowship.Text = fellowship;
            //        TextPerception.Text = perception;
            //        TextIntelligence.Text = intelligence;
            //        TextAgility.Text = agility;
            //        TextWillpower.Text = willpower;
            //        TextToughness.Text = toughness;
            //        TextTalent.Text = talentrequired;
            //        TextSkill.Text = skillrequired;
            //        TextClass.Text = classrequired;

            //    }
            //}

            //    catch (Exception ex)
            //    {
            //        MessageBox.Show("Error: \r\n" + ex);
            //    }

            }

        private void Talents_Load(object sender, EventArgs e)
        {

        }

        }
    }

没有任何事情发生,组合框永远不会被激活,我怎样才能让它发挥作用。

1 个答案:

答案 0 :(得分:1)

我会设置一个你知道在查询中的数据库中存在的静态值,看它是否返回任何内容。这将告诉您查询是否不正确。

 string Query = "SELECT * FROM dark_heresy.talents WHERE TalentName='This.Guy';";

根据您的第二个问题,您可以在Required_Attributes表单中创建静态全局变量,如:

public static string username;
public static string password;

在登录页面的结束事件中,设置变量'值如:

private void Authenticate_Closing(object sender, FormClosingEventArgs e)
    {
        Required_Attributes.username = username.text;
        Required_Attributes.password = password.text; 
    }

您的连接字符串如下所示:

string constring = "datasource = localhost; port = 3306; username =" + username + "; password = " + password + ";";