我在oracle中建了2张表,主表:项目信息;子表:拟定项目信息
主表设id为主键,索引、自增长。子表id也想设置自增,并且用ref_项目信息_id来保存主表id,使得2个表进行数据关联。主表数据是网页的textbox得到,子表数据是在网页中的gridview得到。现在问题是:我在点击子表gridview上的保存时,得不到主表id值。
我的代码:
主表:
public void Button1_OnClick(object sender, EventArgs e)
{
pd = GetCount();
if (pd)
{
Str_insert = @"insert into 申报_GHTJSBB (id,申报人姓名)values(id.NEXTVAL,'" + 申报人姓名.Text + "')
...
}
}子表保存:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string _insert4 = @"insert into XZYJS_NJXM(id,拟建项目,备注,REF_申报_GHTJSBB_ID) values(id.NEXTVAL, '" + (this.GridView1.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox).Text + "','" + (this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text + "',//这块怎么得到主表id)";
DbHelperOra.Query(_insert4);
}
请教下高手,子表的id如何实现自动增长,还有就是怎么得到主表id.
谢谢~!
主表设id为主键,索引、自增长。子表id也想设置自增,并且用ref_项目信息_id来保存主表id,使得2个表进行数据关联。主表数据是网页的textbox得到,子表数据是在网页中的gridview得到。现在问题是:我在点击子表gridview上的保存时,得不到主表id值。
我的代码:
主表:
public void Button1_OnClick(object sender, EventArgs e)
{
pd = GetCount();
if (pd)
{
Str_insert = @"insert into 申报_GHTJSBB (id,申报人姓名)values(id.NEXTVAL,'" + 申报人姓名.Text + "')
...
}
}子表保存:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string _insert4 = @"insert into XZYJS_NJXM(id,拟建项目,备注,REF_申报_GHTJSBB_ID) values(id.NEXTVAL, '" + (this.GridView1.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox).Text + "','" + (this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text + "',//这块怎么得到主表id)";
DbHelperOra.Query(_insert4);
}
请教下高手,子表的id如何实现自动增长,还有就是怎么得到主表id.
谢谢~!
至于你想让子表的id自动增长。 可以在程序里写个自增长的方法呗。
然后再把i, insert 到主表.主表的id就取到了.下面的detail也可以用这个i了.
如果oracle要做自增主键,写一个before insert for each row 触发器就行了.不过还是建议使用业务主键,有好处,比较烦这种没什么意义的id主键.