gpt4 book ai didi

c# - 使用明细表中的外键从 C# 将数据插入 SQL Server 2005 中的主/明细表

转载 作者:太空宇宙 更新时间:2023-11-03 21:52:35 25 4
gpt4 key购买 nike

我需要一些帮助来用 C# 制作订单。我的开发环境是:

  • Microsoft Visual Studio 2010 旗舰版
  • SQL Server Express Edition 2005
  • 编程语言 C#
  • 示例数据库 = NorthWind (表 OrdersOrderDetails)

我已经创建了一个订单输入表单,其中包含一个用于 OrderID 的文本框, Customer 的组合框, OrderDate 的 DateTimePickers和 ShippedDate和一个包含列 OrderID (readonly) 的 DataGridView , ProductID , UnitPrice & Quantity .

在表单加载事件中,我有以下代码:

private void Inv2_Load(object sender, EventArgs e)
{
SetComb();
connectionString = ConfigurationManager.AppSettings["connectionString"];
sqlConnection = new SqlConnection(connectionString);

qryOrd = "Select OrderID, CustomerID, OrderDate, ShippedDate from Orders";
qryOrdDet = "Select OrderID, ProductID, UnitPrice, Quantity from OrderDetails";

sqlConnection.Open();
sqlDataMaster = new SqlDataAdapter(qryOrd, sqlConnection);
sqlDataDet = new SqlDataAdapter(qryOrdDet, sqlConnection);

//SET MASTER INSERT/UPDATES
command = new SqlCommand("INSERT INTO Orders ( CustomerID, OrderDate, ShippedDate) VALUES (@CustID, @OrdDt, @ShipDt) SELECT SCOPE_IDENTITY();");

command.Parameters.Add("@OrdID", SqlDbType.NVarChar, 15);
command.Parameters.Add("@CustID", SqlDbType.VarChar, 15);
command.Parameters["@CustID"].Value = cmbCust.SelectedText;

command.Parameters.Add("@OrdDt", SqlDbType.DateTime);
command.Parameters["@OrdDt"].Value = dtOrdDt.Text;

command.Parameters.Add("@ShipDt", SqlDbType.DateTime);
command.Parameters["@ShipDt"].Value =dtShipDt.Text;

sqlDataMaster.InsertCommand = command;
//string id = command.ExecuteScalar().ToString();

command = new SqlCommand("UPDATE Orders SET CustomerID = @CustID, OrderDate = @OrdDt, ShippedDate = @ShipDt WHERE OrderID = @OrdID");

command.Parameters.Add("@OrdID", SqlDbType.NVarChar, 15, "OrderID").Value = txtOrdID.Text;
command.Parameters.Add("@CustID", SqlDbType.VarChar, 15, "CustomerID").Value = cmbCust.Text;
command.Parameters.Add("@OrdDt", SqlDbType.DateTime).Value = dtOrdDt.Text;
command.Parameters.Add("@ShipDt", SqlDbType.DateTime).Value = dtShipDt.Text;
sqlDataMaster.UpdateCommand = command;

//SET DETAILS INSERT/UPDATES
commandDet = new SqlCommand("INSERT INTO OrderDetails (ProductID, UnitPrice, Quantity) VALUES (@PrdID, @Up,@Qty)");

//commandDet.Parameters.Add("@OrdID", SqlDbType.NVarChar, 15, "OrderID").Value = txtOrdID.Text; ;
commandDet.Parameters.Add("@PrdId", SqlDbType.NVarChar, 5, "ProductID");
commandDet.Parameters.Add("@Up", SqlDbType.VarChar, 50, "UnitPrice");
commandDet.Parameters.Add("@Qty", SqlDbType.VarChar, 20, "Quantity");
sqlDataDet.InsertCommand = commandDet;

commandDet = new SqlCommand("UPDATE OrderDetails SET ProductID = @PrdID, UnitPrice = @Up, Quantity = @Qty WHERE OrderID = @OrdID");
commandDet.Parameters.Add("@OrdID", SqlDbType.NVarChar, 15, "OrderID").Value = txtOrdID.Text; ;
commandDet.Parameters.Add("@PrdId", SqlDbType.NVarChar, 5, "ProductID");
commandDet.Parameters.Add("@Up", SqlDbType.VarChar, 50, "UnitPrice");
commandDet.Parameters.Add("@Qty", SqlDbType.VarChar, 20, "Quantity");
sqlDataDet.UpdateCommand = commandDet;

sqlComBldMaster = new SqlCommandBuilder(sqlDataMaster);
sqlComBldDet = new SqlCommandBuilder(sqlDataDet);

dt = new DataTable();
dtDet = new DataTable();

dt.Clear();
dtDet.Clear();

sqlDataMaster.FillSchema(dt, SchemaType.Source);
sqlDataDet.FillSchema(dtDet, SchemaType.Source);

dtDet.Columns["OrderID"].AutoIncrement = true;
dtDet.Columns["OrderID"].AutoIncrementSeed = -1;
dtDet.Columns["OrderID"].AutoIncrementStep = -1;
ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables.Add(dtDet);
ds.EnforceConstraints = false;

DataRelation rel = new DataRelation("OrdersRel", ds.Tables["Orders"].Columns["OrderID"], ds.Tables["OrderDetails"].Columns["OrderID"]);
ds.Relations.Add(rel);

bs = new BindingSource();
bsDet = new BindingSource();

bs.DataSource = ds;
bs.DataMember = "Orders";

bsDet.DataSource = ds;
bsDet.DataMember = "OrderDetails";

dgInvDet.AutoGenerateColumns = false;

dgInvDet.Columns["ProductID"].DataPropertyName = "ProductID";
ProductID.DataSource = dm.GetData("Select * from Products order by ProductName");
ProductID.DisplayMember = "ProductName";
ProductID.ValueMember = "ProductID";
dgInvDet.Columns["UnitPrice"].DataPropertyName = "UnitPrice";
dgInvDet.Columns["Quantity"].DataPropertyName = "Quantity";

dgInvDet.DataSource = bsDet;
}

public void SetComb()
{
cmbCust.DataSource = dm.GetData("Select * from Customers order by CompanyName");
cmbCust.DisplayMember = "CompanyName";
cmbCust.ValueMember = "CustomerId";
cmbCust.Text = "";
}

Dm.GetData是为检索行而创建的数据访问类方法...

并且在 Save按钮点击事件:

 private void btnSave_Click(object sender, EventArgs e)
{
dt.EndInit();

rec = sqlDataMaster.Update(ds.Tables[0]);
rec += sqlDataDet.Update(ds.Tables[1]);
//recDet = sqlDataDet.Update(dt);

ds.AcceptChanges();

MessageBox.Show(rec + " record(s) applied...." );

ds.EnforceConstraints = true;
}

我需要的是将数据保存到相应表(OrdersOrderDetails)中的 SQL Server,我的代码似乎无法做到这一点。它显示外键不能为空的错误...因为 OrderDetails表还需要 OrderID这是外键,我无法理解如何获得 OrderID ,因为它是在数据插入数据库后自动生成的。

请帮我解决这个问题,将这个外键问题的数据保存在数据库中......

任何帮助将不胜感激。

谢谢

艾哈迈德

最佳答案

我可以在这里想到几个选项。首先,您可以修改主数据适配器上的 SQL 以同时对订单和订单详细信息表执行插入(更新也是如此)并将它们包装在事务中(当然对更新执行相同的操作...)。

另一种选择是处理数据适配器的 RowUpdated 事件(因为它会在插入时触发),获取 ID,然后更新详细信息。

这是我在第一个解决方案中所讨论内容的一个简短示例:

sqlDataMaster = new SqlDataAdapter(qryOrd, sqlConnection);
sqlDataDet = new SqlDataAdapter(qryOrdDet, sqlConnection);

//SET MASTER INSERT/UPDATES

command = new SqlCommand("DECLARE @tempOrderId numeric(38,0);" +
"BEGIN TRAN;" +
"INSERT INTO Orders ( CustomerID, OrderDate, ShippedDate) VALUES (@CustID, @OrdDt, @ShipDt);" +
"SELECT @tempOrderId = SCOPE_IDENTITY();" +
"INSERT INTO OrderDetails (OrderId, ProductID, UnitPrice, Quantity) VALUES (@tempOrderId, @PrdID, @Up,@Qty);"
"IF @@Error <> 0 " +
" ROLLBACK TRANS" +
"ELSE " +
" COMMIT TRANS";
);
command.Parameters.Add("@OrdID", SqlDbType.NVarChar, 15);
command.Parameters.Add("@CustID", SqlDbType.VarChar, 15);
command.Parameters["@CustID"].Value = cmbCust.SelectedText;

command.Parameters.Add("@OrdDt", SqlDbType.DateTime);
command.Parameters["@OrdDt"].Value = dtOrdDt.Text;

command.Parameters.Add("@ShipDt", SqlDbType.DateTime);
command.Parameters["@ShipDt"].Value =dtShipDt.Text;

command.Parameters.Add("@PrdId", SqlDbType.NVarChar, 5, "ProductID");
command.Parameters.Add("@Up", SqlDbType.VarChar, 50, "UnitPrice");
command.Parameters.Add("@Qty", SqlDbType.VarChar, 20, "Quantity");
sqlDataMaster.InsertCommand = command;

关于c# - 使用明细表中的外键从 C# 将数据插入 SQL Server 2005 中的主/明细表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13764817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com