我这有三张表,一张type表,一张project表,还有一张account表.
这三张表存在外键约束关系 type.typeID 1:多 account.typeID project.pno 1:多 account.pno
我要提交到数据库,可是出现这个问题 ForeignKeyConstraint FK_typeID 要求在父表中存在子键值(0)。
下面是我的代码
public partial class 增加收入 : Form
{
SqlConnection conn = DB.DBConnection();
SqlDataAdapter da1;
SqlDataAdapter da2;
SqlDataAdapter da3;
DataSet ds; string type = "";
string money = "";
DateTime dt = DateTime.Now;
string describle = ""; public 增加收入()
{
InitializeComponent();
} private void Form4_Load(object sender, EventArgs e)
{
string typeString = "select * from type";
string accountString = "select * from account";
string projectSTring = "select * from project";
da1 = new SqlDataAdapter(typeString, conn);
da2 = new SqlDataAdapter(accountString, conn);
da3 = new SqlDataAdapter(projectSTring, conn);
ds = new DataSet();
da1.Fill(ds, "type");
da2.Fill(ds, "account");
da3.Fill(ds, "project");
DataRelation relation1 = new DataRelation("FK_typeID", ds.Tables["type"].Columns["typeID"], ds.Tables["account"].Columns["typeID"]);
DataRelation relation2 = new DataRelation("FK_pno", ds.Tables["project"].Columns["pno"], ds.Tables["account"].Columns["pno"]);
ds.Relations.Add(relation1);
ds.Relations.Add(relation2);
ds.EnforceConstraints = true;
BaseClass.FillAttribute(comboBox3);
//BaseClass.FillProject(comboBox5); dateTimePicker2.Value = dt;
} private void button3_Click(object sender, EventArgs e)
{
this.Close();
} private void button4_Click(object sender, EventArgs e)
{
type = comboBox3.SelectedIndex.ToString().Trim();
money = textBox2.Text.Trim();
dt = dateTimePicker2.Value;
describle = richTextBox2.Text.Trim(); DataRow row = ds.Tables["account"].NewRow();
row["rdate"] = dt;
row["typeID"] = type;
row["describle"] = describle;
row["money"] = money;
ds.Tables["account"].Rows.Add(row); SqlCommandBuilder scb1 = new SqlCommandBuilder(da1);
SqlCommandBuilder scb2 = new SqlCommandBuilder(da2);
SqlCommandBuilder scb3 = new SqlCommandBuilder(da3);
da1.Update(ds.Tables["type"]);
da3.Update(ds.Tables["project"]);
da2.Update(ds.Tables["account"]);
ds.AcceptChanges();
MessageBox.Show("提交成功", "老树提醒");
} private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
BaseClass.FillProject(comboBox3, comboBox5);
}
}本人现在非常急,急着交作业,哪位赶紧看下啊~提个意见
谢谢
这三张表存在外键约束关系 type.typeID 1:多 account.typeID project.pno 1:多 account.pno
我要提交到数据库,可是出现这个问题 ForeignKeyConstraint FK_typeID 要求在父表中存在子键值(0)。
下面是我的代码
public partial class 增加收入 : Form
{
SqlConnection conn = DB.DBConnection();
SqlDataAdapter da1;
SqlDataAdapter da2;
SqlDataAdapter da3;
DataSet ds; string type = "";
string money = "";
DateTime dt = DateTime.Now;
string describle = ""; public 增加收入()
{
InitializeComponent();
} private void Form4_Load(object sender, EventArgs e)
{
string typeString = "select * from type";
string accountString = "select * from account";
string projectSTring = "select * from project";
da1 = new SqlDataAdapter(typeString, conn);
da2 = new SqlDataAdapter(accountString, conn);
da3 = new SqlDataAdapter(projectSTring, conn);
ds = new DataSet();
da1.Fill(ds, "type");
da2.Fill(ds, "account");
da3.Fill(ds, "project");
DataRelation relation1 = new DataRelation("FK_typeID", ds.Tables["type"].Columns["typeID"], ds.Tables["account"].Columns["typeID"]);
DataRelation relation2 = new DataRelation("FK_pno", ds.Tables["project"].Columns["pno"], ds.Tables["account"].Columns["pno"]);
ds.Relations.Add(relation1);
ds.Relations.Add(relation2);
ds.EnforceConstraints = true;
BaseClass.FillAttribute(comboBox3);
//BaseClass.FillProject(comboBox5); dateTimePicker2.Value = dt;
} private void button3_Click(object sender, EventArgs e)
{
this.Close();
} private void button4_Click(object sender, EventArgs e)
{
type = comboBox3.SelectedIndex.ToString().Trim();
money = textBox2.Text.Trim();
dt = dateTimePicker2.Value;
describle = richTextBox2.Text.Trim(); DataRow row = ds.Tables["account"].NewRow();
row["rdate"] = dt;
row["typeID"] = type;
row["describle"] = describle;
row["money"] = money;
ds.Tables["account"].Rows.Add(row); SqlCommandBuilder scb1 = new SqlCommandBuilder(da1);
SqlCommandBuilder scb2 = new SqlCommandBuilder(da2);
SqlCommandBuilder scb3 = new SqlCommandBuilder(da3);
da1.Update(ds.Tables["type"]);
da3.Update(ds.Tables["project"]);
da2.Update(ds.Tables["account"]);
ds.AcceptChanges();
MessageBox.Show("提交成功", "老树提醒");
} private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
BaseClass.FillProject(comboBox3, comboBox5);
}
}本人现在非常急,急着交作业,哪位赶紧看下啊~提个意见
谢谢
[email protected]