我有一个父表ParentTable
,它有一个主键。我想将数据插入ParentTable
,然后使用该主键将行插入子表ChildTable
。
实施例
ParentTable :
Id ClassName
----------------
1 MailClass
2 HelperClass
3 DataClass
ChildTable :
ChildId Id Details
---------------------------
200 1 this is for Main Class
201 1 this is for Main Class
203 2 this is for Helper Class
因此,如果将id = 3
添加到ParentTable
,我希望将id = 3
的行插入ChildTable
,依此类推......
这里我有两个DataTable
s - dtParentTable
和dtChild
。必须使用ParentTable
生成scope_identity
id,并且必须将此ID插入子表
我们必须使用adapter.Update();
来实现此目标
尝试使用此功能:https://msdn.microsoft.com/en-us/library/ks9f57t0(v=vs.110).aspx
代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace checkScopIdentity
{
class Program
{
static void Main(string[] args)
{
string strConn1 = "Data Source=CS40-PC;Initial Catalog=DBName;User ID=sa;Password=root";
using (SqlConnection conn = new SqlConnection(strConn1))
{
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT Id, InterfaceName FROM ParenTable",conn);
adapter.InsertCommand = new SqlCommand(
"INSERT INTO ParenTable (InterfaceName) " +
"VALUES (@InterfaceName); " +
"SELECT Id, InterfaceName FROM ParenTable " +
"WHERE Id = SCOPE_IDENTITY();", conn);
adapter.InsertCommand.Parameters.Add(
new SqlParameter("@InterfaceName", SqlDbType.NVarChar, 40,
"InterfaceName"));
adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.Both;
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
DataTable ParenTableFinal = new DataTable();
adapter.Fill(ParenTableFinal);
DataRow newRow = ParenTableFinal.NewRow();
newRow["InterfaceName"] = "New Shipper";
ParenTableFinal.Rows.Add(newRow);
DataTable dataChanges = new DataTable();
dataChanges = ParenTableFinal.GetChanges();
adapter.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);
adapter.Update(dataChanges);
// second Table
DataSet ds = new DataSet();
DataTable dtInbound = new DataTable();
SqlDataAdapter adapterChild = new SqlDataAdapter("SELECT Id,InnId,Name,Contact FROM InnBoundTable", conn);
adapterChild.FillSchema(dtInbound, SchemaType.Source);
dtInbound.Rows.Add(null,null,"Yash","Fale");
dtInbound.GetChanges();
ds.Tables.Add(dataChanges);
ds.Tables.Add(dtInbound);
ds.EnforceConstraints = false;
DataRelation dRelation ;
dRelation = ds.Relations.Add("info", ds.Tables["ParenTable"].Columns["Id"], ds.Tables["InnBoundTable"].Columns["Id"]);
dRelation.ChildKeyConstraint.UpdateRule = Rule.Cascade;
ds.AcceptChanges();
ds.GetChanges();
Console.WriteLine("Rows after merge.");
foreach (DataRow row in dtInbound.Rows)
{
{
Console.WriteLine("{0}: {1}", row[0], row[1]);
}
}
conn.Close();
}
Console.ReadKey();
}
private static void OnRowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
if (e.StatementType == StatementType.Insert)
{
e.Status = UpdateStatus.SkipCurrentRow;
}
}
}
}
// ParenTable
CREATE TABLE [dbo].[ParenTable](
[Id] [int] IDENTITY(1,1) NOT NULL,
[InterfaceName] [nvarchar](50) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[InnBoundTable](
[Id] [int] NULL,
[InnId] [int] NULL,
[Name] [nchar](10) NULL,
[Contact] [nchar](10) NULL
) ON [PRIMARY]
这里尝试获取parentTable Id在“InnBoundTable”中但没有反映任何更改,在InnBoundTable的Id中显示空值,即childTable