VisualStudio Visual C#
Windowsフォームアプリケーション
Microsoft SQL Server Management Studio
環境でシステムを作ってます。
Formにあるcombobox1の内容をデータベースから取得してます。combobox2も同様にしたいと思っていますが上手くいきません。また、combobox1とcombobox2は、データベースが異なります。よろしくお願いします!
同じような文を連なって書いた場合やif文で回したりしましたができませんでした。
private void Form_Load(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = @"Data Source 〜 ";
SqlCommand Cd = new SqlCommand();
cn.Open(); cd.Connection = cn; cd.CommandText = "Select * From Table"; SqlDataReader re = cd.ExecuteReader(); re.Read(); if(re.HasRows == false) { MessageBox.Show("ありません"); } else { cn.Close(); re.Close(); cn.Open(); cd = new SqlCommand("Select Table_Code From Table", cn); re = cd.ExecuteReader(); while(re.Read()) { combobox1.Items.Add(re.GetString(0)); } re.Close(); cd.Connection.Close(); cn.Close(); }