最初に、私はC#に非常に慣れていないため、お詫び申し上げます。これはばかげた質問のように聞こえるかもしれません。ただし、インターネット上のどこでも、または購入したC#に関する数冊の本の中にも例を見つけることができませんでした
SQL データベースからロード時に大量のデータを取得するフォームがあります。ファーストネームとラストネームは、2 つの別個のコンボボックスに挿入され、1 つは「First Last」として情報を表示し、もう 1 つは「Last, First」として情報を表示し、名前を簡単に参照できるようにします。
先日、ここで助けてくれたおかげで、単一のテキストボックスにコンボボックスの選択した値を入力する方法を学びました。助けてくれてありがとう。
私がする必要があるのは、ユーザーがコンボボックスから選択されたときに、そのユーザーのすべての情報がフォームの特定のテキストボックス内に表示されることです。例は、
LastName = textBox1
FirstName = textBox2
MiddleName = textBox3
これらのフィールドごとに個別の文字列を記述する必要がありますか?それとも、すべてのデータについて SQL データベースにクエリを実行する 1 つの文字列からそのデータを取得できますか?
ご提供いただけるご支援をいただければ幸いです。
これが私がこれまでに持っているものです
enter code here
namespace Tempus.Menus
{
public partial class Employees : Form
{
public Employees()
{
InitializeComponent();
//Connect to database for Employees Table Headers
SqlConnection myConnection = new SqlConnection(@"Server=Server4\INSTANCE;Integrated Security=true;" +
"user id=userID;password=password;" +
"Trusted_Connection=yes;" +
"Database=Database;" +
"connection timeout=30");
try
{
myConnection.Open();
string SqlDataPull = String.Format("SELECT * FROM Employees WHERE Lname IS NOT NULL {0}", (checkBox1.Checked ? "AND Active='Y'" : ""));
//string SqlDataPull2 = String.Format("SELECT * FROM Employees WHERE Fname IS NOT NULL {0} ORDER By Fname", (checkBox1.Checked ? "AND Active='Y'" : ""));
SqlCommand cmd = new SqlCommand(SqlDataPull, myConnection);
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string strEmployee = String.Format("{0} {1}", dr["Fname"], dr["Lname"], dr["Mname"], dr["DOH"], dr["DOT"], dr["Active"], dr["DoNotRehireReason"], dr["PTO-balance"], dr["SNP-balance"], dr["Cred"]);
comboBox1.Items.Add(strEmployee);
string strEmployee2 = String.Format("{0}, {1}", dr["Lname"], dr["Fname"]);
comboBox2.Items.Add(strEmployee2);
int Fname = dr.GetInt32(0);
string firstName = String.Format("{0}", dr["Fname"]);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
if (myConnection != null)
{
myConnection.Dispose();
}
}
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
comboBox2.SelectedIndexChanged += comboBox2_SelectedIndexChanged;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == -1)
{
textBox1.Text = string.Empty;
}
else
{
textBox1.Text = comboBox1.SelectedItem.ToString();
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.SelectedIndex == -1)
{
textBox1.Text = string.Empty;
}
else
{
textBox1.Text = comboBox2.SelectedItem.ToString();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Employees_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Main myNewForm = new Main();
myNewForm.Show();
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
Reports myNewForm = new Reports();
myNewForm.Show();
this.Close();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void textBox8_TextChanged(object sender, EventArgs e)
{
}
private void textBox9_TextChanged(object sender, EventArgs e)
{
}
private void textBox7_TextChanged(object sender, EventArgs e)
{
}
private void textBox10_TextChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
// logic here for if the box has now been checked
}
else
{
// what to do if the box has been unchecked
}
}
}
}