問候程序員,C#我如何從數據庫中獲取數據,文本框
我有一個組合框,在我的Windows中的一些文本框狀地連接到一個數據庫。我的想法是,當組合框的值改變文本框中的數據時也會發生變化。就像當我在組合框中選擇客戶ID時,文本框將加載客戶的數據,例如全名或地址等。但是,當我打開表單時,組合框disapear和文本框保持空白。我正在使用Access數據庫和Visual Studio 2012進行編碼。這裏是我的代碼..
C#代碼:
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();
}
}
W00t comboBox disapears?你有沒有將comboBox屬性設置爲false? – Obama 2013-04-10 11:38:17
你在form_Load事件中有什麼代碼? – StackTrace 2013-04-10 11:45:50
不,它只是消失 – user2241062 2013-04-10 11:46:09