一个字段中有多个值(外键?) [英] Multiple values in one field (foreign keys?)

查看:310
本文介绍了一个字段中有多个值(外键?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试订购系统,您可以在一个订单中放置许多产品。我对这一点知之甚少,现在我正在

有3个表格,Product表格,Order表格和订购产品表。
我真的不知道这是否正确,因为我是初学者,尤其是外键。

我想要达到的是您可以订购许多产品和把这个产品放到一个OrderID中,如下图所示。



这是我唯一的代码。对不起,但我真的迷失在这。

  public Form1()
{
InitializeComponent();
fillCart();


$ b $ private void fillCart()
{
dgvCart.ColumnCount = 3;
dgvCart.Columns [0] .Name =ProductID;
dgvCart.Columns [1] .Name =ProductName;
dgvCart.Columns [2] .Name =Quantity;

$ b $ private void dataGridView1_CellClick(object sender,DataGridViewCellEventArgs e)
{
// dgvproducts
}

private void Form1_Load(object sender,EventArgs e)
{
crud.FillDataGrid(Select * from Products,ref dgvProducts);
crud.FillDataGrid(Select * from Orders,ref dgvOrder);
crud.FillDataGrid(Select * from Orderproducts,ref dgvOrderview);
lbldate.Text = DateTime.Now.ToShortDateString();

$ b $ private void button2_Click(object sender,EventArgs e)
{
//按钮添加到购物车
addData(dgvProducts.CurrentRow.Cells [ 0] .Value.ToString(),dgvProducts.CurrentRow.Cells [1] .Value.ToString(),txtqty.Text);


private void addData(string p1,string p2,string p3)
{
String [] row = {p1,p2,p3};
dgvCart.Rows.Add(row);

$ b $ private void button1_Click(object sender,EventArgs e)
{
//按钮插入

}

非常感谢您,我希望有人可以帮我解决我的问题。


$ b $使用SQL Server 2008填充datagridview的方法:

  public crud()
{
cnString =Data Source = DESKTOP-MQKIBSK\\\SQLEXPRESS; Initial Catalog = MARISCHELLdatabase; Integrated Security = True;
cn = new SqlConnection(cnString);



$ b public void FillDataGrid(string sql,ref ns1.BunifuCustomDataGrid dg)
{
try
{
DataSet ds = new DataSet();
cn.Open();
cmd = new SqlCommand(sql,cn);
adptr = new SqlDataAdapter(cmd);
adptr.Fill(ds);
dg.DataSource =;
dg.DataSource = ds.Tables [0];


$ b catch(Exception e)
{
MessageBox.Show(+ e.Message);
}
cn.Close();


解决方案

  • Linq 2 SQL数据类对我来说:



    使用它的代码:

      //我在dataGridView中有2列,Id第一个数量第二个
    //我添加了3个项目用于测试
    List< Tuple< int, INT>> cart = new List< Tuple< int,int>>();
    foreach(DataGridViewRow row dataGridView1.Rows)
    {
    if(row.Cells [0] .Value!= null&&& row.Cells [1] .Value!= null )
    {
    cart.Add(new Tuple< int,int>(Convert.ToInt32(row.Cells [0] .Value.ToString()),Convert.ToInt32(row.Cells [1] .Value.ToString())));
    //现在每个列表项都会有.Item1(productId)和.Item2(amount)
    }
    }
    using(DataClasses1DataContext dataContext = new DataClasses1DataContext())
    {
    //你在dataContext中添加的表可以通过名称
    来访问。Order order = new Order();
    dataContext.Orders.InsertOnSubmit(order);
    dataContext.SubmitChanges(); //提交一次,所以我们得到一个orderId
    foreach(元组中的元组< int,int>产品)
    {
    OrderProduct orderProduct = new OrderProduct();
    orderProduct.OrderId = order.OrderID;
    orderProduct.ProductId = product.Item1;
    orderProduct.Amount = product.Item2;
    dataContext.OrderProducts.InsertOnSubmit(orderProduct);
    }
    dataContext.SubmitChanges();
    }


    I am trying to do an Ordering System where you can put many Products in one order. I have very little knowledge about this and this is where i am now

    There are 3 tables, Product table,Order table and the Order-products table. I really don't know if this is right as i am beginner especially on foreign keys.

    What I want to achieve is you can order many products and put that products into one "OrderID" like this example in pic below.

    This are my only codes. Sorry but i am really lost at this.

        public Form1()
        {
            InitializeComponent();
            fillCart();
        }
    
    
        private void fillCart()
        {
            dgvCart.ColumnCount = 3;
            dgvCart.Columns[0].Name = "ProductID";
            dgvCart.Columns[1].Name = "ProductName";    
            dgvCart.Columns[2].Name = "Quantity";
        }
    
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //dgvproducts
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
           crud.FillDataGrid("Select * from Products", ref dgvProducts);
           crud.FillDataGrid("Select * from Orders", ref dgvOrder);
           crud.FillDataGrid("Select * from Orderproducts", ref dgvOrderview);
           lbldate.Text = DateTime.Now.ToShortDateString();
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            //button add to cart
            addData(dgvProducts.CurrentRow.Cells[0].Value.ToString(), dgvProducts.CurrentRow.Cells[1].Value.ToString(), txtqty.Text);
        }
    
        private void addData(string p1, string p2, string p3)
        {
            String[] row = { p1, p2, p3 };
            dgvCart.Rows.Add(row);
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            //button insert
    
        }
    

    Thank you very much and i hope someone can help me with my problem.

    Method use for filling datagridview from SQLserver 2008:

          public crud()
        {
            cnString = "Data Source=DESKTOP-MQKIBSK\\SQLEXPRESS;Initial Catalog=MARISCHELLdatabase;Integrated Security=True";
            cn = new SqlConnection(cnString);
        }
    
    
    
        public void FillDataGrid(string sql, ref ns1.BunifuCustomDataGrid dg)
        {
            try
            {
                DataSet ds = new DataSet();
                cn.Open();
                cmd = new SqlCommand(sql, cn);
                adptr = new SqlDataAdapter(cmd);
                adptr.Fill(ds);
                dg.DataSource = "";
                dg.DataSource = ds.Tables[0];
    
    
            }
            catch (Exception e)
            {
                MessageBox.Show("" + e.Message);
            }
            cn.Close();
        }
    

    解决方案

    How Linq 2 SQL dataclasses look for me:

    The code that goes with it:

    //I have 2 columns in my dataGridView, Id 1st amount 2nd
    //I added 3 items for testing
    List<Tuple<int, int>> cart = new List<Tuple<int,int>>();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.Cells[0].Value != null && row.Cells[1].Value != null)
        { 
            cart.Add(new Tuple<int, int>(Convert.ToInt32(row.Cells[0].Value.ToString()),Convert.ToInt32(row.Cells[1].Value.ToString())));
                    //Now each list item will have .Item1 (productId) and .Item2 (amount)
        }
    }
    using (DataClasses1DataContext dataContext = new DataClasses1DataContext())
    {
        //The tables you add in the dataContext are accessible by name
        Order order = new Order();
        dataContext.Orders.InsertOnSubmit(order);
        dataContext.SubmitChanges(); // Submit once so we get an orderId
        foreach (Tuple<int, int> product in cart)
        {
            OrderProduct orderProduct = new OrderProduct();
            orderProduct.OrderId = order.OrderID;
            orderProduct.ProductId = product.Item1;
            orderProduct.Amount = product.Item2;
            dataContext.OrderProducts.InsertOnSubmit(orderProduct);
        }
        dataContext.SubmitChanges();
    }
    

    这篇关于一个字段中有多个值(外键?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆