인사말 프로그래머,C# 데이터베이스에서 텍스트 상자로 데이터를 가져 오는 방법
내 Windows 양식에 데이터베이스에 연결된 일부 텍스트 상자와 콤보 박스가 있습니다. 내 생각에, 콤보 상자의 값이 변경되면 텍스트 상자의 데이터도 변경됩니다. 콤보 상자에서 고객 ID를 선택할 때와 마찬가지로 성명이나 주소 등 고객의 데이터가 텍스트 상자에로드됩니다. 그러나 양식을 열면 콤보 박스가 사라지고 텍스트 상자가 비어있게됩니다. Access 데이터베이스와 Visual Studio 2012를 사용하여 코드를 작성하고 있습니다. 데이터라는
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
string pc = Convert.ToString(comboBox1.SelectedValue);
string sql = "SELECT * FROM Customer WHERE CustomerID="+ pc;
Data.Customer_Data(sql, pc);
txtfullname.Text = Data.fullname;
txtadress.Text = Data.adress;
txtcity.Text = Data.city;
txtemail.Text = Data.email;
}
그리고 내 클래스 :
public static string cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\Database1.accdb";
public static string fullname = "";
public static string adress = "";
public static string city = "";
public static string email = "";
public static void Customer_Data(string sql, string pc)
{
if (pc == "")
{
return;
}
else
{
OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(cs);
oleDbConnection1.Open();
OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand(sql,oleDbConnection1);
OleDbDataReader reader = oleDbCommand1.ExecuteReader();
if (!reader.Read())
return;
fullname = reader["fullname"].ToString();
adress = reader["adress"].ToString();
city = reader["city"].ToString();
email = reader["email"].ToString();
oleDbConnection1.Close();
}
}
콤보 상자가 사라집니다. comboBox 속성을 false로 표시하도록 설정 했습니까? – Obama
form_Load 이벤트에 어떤 코드가 있습니까? – StackTrace
아니요, 그냥 사라집니다 – user2241062