【问题标题】:Unable to Relate DataTable with another C#无法将 DataTable 与另一个 C# 相关联
【发布时间】:2013-03-06 10:54:02
【问题描述】:

所以,我知道您通过相关列关联 DataTables...虽然我有一个小问题。

例子:

父表 - 客户 -> {customerName, customerCode(PK), telphoneCell, ...等}

子表 - 订单 -> { customerCode, orderCode(PK) dateStart, dateEnd, ... etc}

现在...

孙表 - ManufacturingSheet -> {panelNumber, panelWidth, panelHeight, ...等}

还有一些文本框显示了我根据用户输入计算得出的零件数量,例如螺栓和螺母。

那么如何将整个表单保存到单个客户的订单中?客户名称、代码和日期详细信息等也出现在此表格上。

即使我可以将 texbox 链接到 Orders 表,我如何将它链接到表单的其余部分?

这是显示我想要存储在数据库中的所有网格和文本框的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PalisadeWorld
{
public partial class ManufacturingSheet : Form
{
    public ManufacturingSheet()
    {
        InitializeComponent();
    }

    //all the calculated sizes, and parts
    public string[] dis_Width { get; set; }
    public string[] dis_Height { get; set; }
    public string[] dis_Comments { get; set; }
    public int[] dis_PaleQty { get; set; }
    public int[] dis_Blocks { get; set; }
    public int dis_num { get; set; }

    //Method to Convert a string array to int array
    private int[] ConvertArray(string[] s, int rows)
    {
        int[] intArray = new int[rows];

        for (int x = 0; x < rows; x++)
        {
            intArray[x] = Convert.ToInt32(s[x]);
        }
        return intArray;
    }

    //Methods returning other parts calculated with sizes above
    private int GetTotalBearers(string[] bearers, int rows)
    {
        int i;
        int totalBearers = 0;
        int[] temp = ConvertArray(bearers, rows);
        for (i = 0; i < rows; i++)
        {
            if (temp[i] >= 2200)
            {
                totalBearers += 6;
            }
            else totalBearers += 4;
        }
        return totalBearers;
    }
    private int GetTotalPales(int[] pales, int rows)
    {
        int i;
        int totalPales = 0;
        for (i = 0; i < rows; i++)
        {
            totalPales += pales[i];
        }
        return totalPales;
    }
    private int GetTotalBoltsNutsBrackest(int[] pales, int rows)
    {
        int totalBolts = GetTotalPales(pales, rows) + (rows * 4);
        return totalBolts;
    }

    private void ManufacturingSheet_Load(object sender, EventArgs e)
    {
        //displaying number of respective parts
        numBearers.Text = string.Format("{0}", GetTotalBearers(dis_Height, dis_num));
        numPales.Text = string.Format("{0}", GetTotalPales(dis_PaleQty, dis_num));
        numBrackets.Text = numBearers.Text;
        numBoltsNutsWashers.Text = string.Format("{0}", GetTotalBoltsNutsBrackest(dis_PaleQty, dis_num));

        int i;
        int no = 0;

        //displaying sizes etc in DataGrid
        for(i = 0; i < dis_num; i++)
        {
            // Create a new row.
            PalisadeWorldDatabaseDataSet.ManufacturingSheetRow newOutputRow;

            newOutputRow = palisadeWorldDatabaseDataSet1.ManufacturingSheet.NewManufacturingSheetRow();

            newOutputRow.no = ++no ;
            newOutputRow.cutSize = string.Format("{0}", dis_Width[i]);
            newOutputRow.block = string.Format("{0}", dis_Blocks[i]);
            newOutputRow.height = string.Format("{0}", dis_Height[i]);
            newOutputRow.palesQty = string.Format("{0}", dis_PaleQty[i]);
            newOutputRow.comments = string.Format("{0}", dis_Comments[i]);

            // Add the row to the Region table 
            this.palisadeWorldDatabaseDataSet1.ManufacturingSheet.Rows.Add(newOutputRow);

            // Save the new row to the database 
            this.manufacturingSheetTableAdapter.Update(this.palisadeWorldDatabaseDataSet1.ManufacturingSheet);

            this.manufacturingSheetTableAdapter.Fill(this.palisadeWorldDatabaseDataSet1.ManufacturingSheet);
        }           
    }
}

}

【问题讨论】:

  • 也许ManufacturingSheet更像是一个报告而不是一个数据集,但它仍然需要保存在客户的详细信息下

标签: c# sql database visual-studio-2010


【解决方案1】:

DataAdapter 允许您定义多个表及其关系,并为每个表定义插入和更新命令(以及选择命令和删除命令)。

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx

至于 GUI 问题,通常您会有 OrderItems 表(通常命名为 OrderDetail),并且您会使用 DataGrid 来捕获订单上的一个或多个项目:

        OrderItems
        OrderID
        ItemID   typically references a Parts table
        Quantity
        UnitPrice

【讨论】:

  • 通常是的。但是这些面板都是相同的产品,只是尺寸不同,通常尺寸相同。而且使用 panelNumber 作为参考是没有用的,因为它只是一个计数器
猜你喜欢
  • 2021-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
  • 1970-01-01
  • 2021-07-16
  • 1970-01-01
相关资源
最近更新 更多