//这是插入代码
private void button4_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Andrej\Desktop\B9PROBA\B9\B9\Database5.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand com = new SqlCommand("INSERT INTO ARB5Stadion(StadionID,Naziv,Adresa,Kapacitet,BrojUlaza,GradID ) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + comboBox1.ValueMember + "')", conn);
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
MessageBox.Show("Uspesno Uneti Podaci");
}
catch (Exception e1) { MessageBox.Show(e1.Message); }
finally { conn.Close(); }
//这是组合框的代码,它从一个数据表中读取,使用 id 值作为值成员,名称作为显示成员。我基本上试图将 ARB5Grad 表的 GradID 主键插入到 StadionID 中GradID 是外键
private void Stadion_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Andrej\Desktop\B9PROBA\B9\B9\Database5.mdf;Integrated Security=True;Connect Timeout=30");
conn.Open();
SqlCommand sc = new SqlCommand("SELECT GradID,Grad FROM ARB5Grad ", conn);
SqlDataReader reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("GradID", typeof(int));
dt.Columns.Add("Grad", typeof(string));
dt.Load(reader);
comboBox1.ValueMember = "GradID";
comboBox1.DisplayMember = "Grad";
comboBox1.DataSource = dt;