使用C#将带有外键的2个表插入到SQL Server中

时间:2017-12-01 03:36:10

标签: c# sql-server

我想知道C#的查询,我想在其中插入带有外键的2个表。我使用的当前代码:

SqlConnection con = new SqlConnection(WindowsFormsApp1.Properties.Resources.connection.ToString());
con.Open();

SqlCommand com = new SqlCommand("Begin TRANSACTION INSERT INTO daftar_buku values('"+Code.Text+"','"+Judul.Text+"','"+Kategori.Text+"','"+Nomor.Text+"') insert into stock_buku(nama_buku, stok_buku) values ((select judul_buku from daftar_buku where judul_buku ='"+Judul.Text+"'),'"+Stock.Text+"') COMMIT ",con);

SqlDataAdapter da = new SqlDataAdapter("select judul_buku from daftar_buku where judul_buku = '"+Judul.Text+"'",con);

DataTable dt = new DataTable();
da.Fill(dt);

if (dt.Rows.ToString() == Judul.Text)
{
    MessageBox.Show("Data Added!");
    this.Hide();
    Refresh();
}
else
{
    MessageBox.Show("Failed To add Book!","Information");
}

但该代码不起作用......

请帮帮我

0 个答案:

没有答案