C# C Mysql链接组合框,文本框中包含数据库值
我有一个表单,它有一个组合框,必须显示名称,当名称为select时,必须在不同的文本框中显示mysql数据库中的不同值C# C Mysql链接组合框,文本框中包含数据库值,c#,mysql,combobox,database-connection,C#,Mysql,Combobox,Database Connection,我有一个表单,它有一个组合框,必须显示名称,当名称为select时,必须在不同的文本框中显示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; usin
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);
}
}
}
}
但是,当我打开表单并按下组合框时,什么都没有发生,什么都没有显示,没有语法错误发生,代码出了什么问题
即使我将代码更改为:
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)
{
}
}
}
什么都没有发生,组合框从来没有启动过,我如何使它工作。我会设置一个静态值,您知道该值存在于查询中的数据库中,并查看它是否返回任何内容。这将告诉您您的查询是否不正确
string Query = "SELECT * FROM dark_heresy.talents WHERE TalentName='This.Guy';";
根据第二个问题,您可以在所需的_属性表单中创建静态全局变量,如:
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 + ";";
你是否逐行调试代码并查看发生了什么?是的,它从不同的名称中获取值,但之后什么也没有发生你确定你的查询会在数据库管理器中返回数据吗?@SonerGönül嗯,信息在dark_heresy.Talenties,当我在mysql工作台中运行它时,会显示表,嗯,可能是在组合框中没有加载名称,这就是问题所在吗?@SonerGönül我仍然无法理解为什么它没有显示任何内容,我现在尝试创建一个静态值来显示,但什么都没有发生。当我在mysql工作台中执行samme命令时,它会显示详细信息。我现在已经尝试了第21行中的set debug:private void cb_talients_SelectedIndexChangedobject发送方EventArgs e{,它从来没有达到这样的程度,没有运行调试更改它,实际上没有显示任何内容。但是,当我在mysqlworkbench中运行相同的代码时,从dark_heresy.talents中选择*FROM dark_heresy.talents,其中TalentName='AmbidLextous';我得到一个结果,我有一个更新,似乎combox框有问题,因为我已将代码更改为just simple:private void cb_Talenties_SelectedIndexChangedobject发送者,EventArgs e{MessageBox.ShowTest;}但什么也没有发生!我将删除所选索引更改的事件。然后转到“设计”视图,双击组合框以自动生成事件。再次尝试MessageBox.ShowTestl并查看它的功能。确保您确实在组合框中单击了其他内容,否则事件将不会触发。我已尝试并如果什么都没有发生,它就不会生成它。我现在在初始化组件cb_Talents后添加了一个eventhandler。SelectedIndexChanged+=新EventHandlercb_Talents_SelectedIndexChanged;但是什么都没有发生如果您构建代码,它是否包含错误?visual studio没有为您自动生成该方法是没有意义的。在设计中gn视图如果右键单击控件并点击属性,然后单击属性窗口中的闪电图标,它是否列出selectedindexchanged设置为您创建的方法?