简体   繁体   English

使用具有外键的表将数据从asp.net页面插入到我的数据库中

[英]insert data from asp.net page to my database with tables which have foreign key

I'm a beginner asp.net programmer and my project is online shopping classes I have some problem in it 我是初学的asp.net程序员,我的项目是在线购物课我有一些问题

I have 4 tables with some foreign key between them... 我有4张表,其间有一些外键......

CREATE TABLE [dbo].[orderdetails] 
(
    [orderid] INT NOT NULL,
    [classid] INT NOT NULL,

   CONSTRAINT [PK_orderdetails] 
      PRIMARY KEY CLUSTERED ([orderid] ASC, [classid] ASC)
);

CREATE TABLE [dbo].[order] 
(
    [orderid]    INT IDENTITY (300, 1) NOT NULL,
    [customerid] INT NOT NULL,

    CONSTRAINT [PK_order] 
       PRIMARY KEY CLUSTERED ([orderid] ASC)
);

CREATE TABLE [dbo].[customer] 
(
    [customerid] INT IDENTITY (200, 1) NOT NULL,
    [firstname]  NVARCHAR (50) NOT NULL,
    [lastname]   NVARCHAR (50) NOT NULL,
    [phone]      INT           NOT NULL,

    CONSTRAINT [PK_Table_1] 
       PRIMARY KEY CLUSTERED ([customerid] ASC)
);

CREATE TABLE [dbo].[class] 
(
    [classid]    INT IDENTITY (100, 1) NOT NULL,
    [numofclass] INT NOT NULL,
    [numofstud]  INT NOT NULL,
    [totalprice] INT NOT NULL,

    CONSTRAINT [PK_class] 
       PRIMARY KEY CLUSTERED ([classid] ASC)
);

FK_orderdetails_order
FK_order_customer
FK_orderdetails_class

I have three pages and in first page I pass some data to another page and in second page I set my data to my DB. 我有三个页面,在第一页中我将一些数据传递给另一个页面,在第二页中我将数据设置为我的数据库。

First page code 第一页代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;  

public partial class _Default : System.Web.UI.Page
{
       static int totalprice = 0;

       protected void Page_Load(object sender, EventArgs e)
       {
           int studprice = Convert.ToInt32(Numofstud.SelectedValue) * 6;
           int classprice = Convert.ToInt32(Numofclass.SelectedValue) * 190;

           totalprice = studprice + classprice;
           lblTotalprice.Text = string.Format("{0}", totalprice);
       }

       protected void Numofstud_SelectedIndexChanged(object sender, EventArgs e)
       {
           int studprice = Convert.ToInt32(Numofstud.SelectedValue) * 6;
           int classprice = Convert.ToInt32(Numofclass.SelectedValue) * 190;

           totalprice = studprice + classprice;
           lblTotalprice.Text = string.Format("{0}", totalprice);
       }

       protected void Numofclass_SelectedIndexChanged(object sender, EventArgs e)
       {
                int studprice = Convert.ToInt32(Numofstud.SelectedValue) * 6;
                int classprice = Convert.ToInt32(Numofclass.SelectedValue) * 190;

                totalprice = studprice + classprice;
                lblTotalprice.Text = string.Format("{0}", totalprice);
            }

            protected void Registerbtn_Click(object sender, EventArgs e)
            {
                Session["Numofclass"] = Numofclass.SelectedItem.Value;
                Session["totalprice"] = totalprice;
                Session["Numofstud"] = Numofstud.SelectedItem.Value;

                Response.Redirect("account.aspx");
            }  
        }

Second page code: 第二页代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;  


public partial class account : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void buybtn_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["miztahrirtest2DB"].ToString());

        SqlCommand cmd = new SqlCommand("insert into customer (firstname, lastname, phone) values (@firstname, @lastname, @phone)", con);
        cmd.Parameters.AddWithValue("firstname", firstnametxt.Text);
        cmd.Parameters.AddWithValue("lastname", lastnametxt.Text);
        cmd.Parameters.AddWithValue("phone", phonetxt.Text);

        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

        SqlCommand cmd2 = new SqlCommand("insert into class (numofstud, numofclass, totalprice) values (@numofstud, @numofclass, @totalprice)", con);
        cmd2.Parameters.AddWithValue("numofclass", Session["Numofclass"]);
        cmd2.Parameters.AddWithValue("numofstud", Session["Numofstud"]);
        cmd2.Parameters.AddWithValue("totalprice", Session["totalprice"]);

        con.Open();
        cmd2.ExecuteNonQuery();
        con.Close();

        SqlCommand cmd3 = new SqlCommand("insert into order ....

        Response.Redirect("bank.aspx");
    }
}

My problem is that I don't know how to insert value into tables which have foreign key and primary key.cmd1 and cmd2 are working correctly but I can't write something to set order table and orderdetails table which They have a foreign key from another table... 我的问题是我不知道如何将值插入到具有外键和主键的表中.cm1和cmd2正常工作但是我不能写一些东西来设置订单表和orderdetails表它们有一个外键来自另一张桌子......

When you inserting parent table you should put scope_identity() end of insert command. 插入父表时,应该将insert_identity()结束插入命令。 And you should use execute scalar. 你应该使用执行标量。 Scope identity give you inserted id by execute scalar. 范围标识通过执行标量为您提供插入的id。 After then you can insert child items with this parent id. 之后,您可以插入具有此父ID的子项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试将 ASP.NET Webform 中的数据插入数据库。 但是我得到一个外键错误 - I'm trying to insert data from a ASP.NET Webform into database. But I get a foreign key error 如何使用 c# asp.net 将外键数据插入数据库 - How to insert foreign key data into database using c# asp.net 如何从 ASP.NET CORE 外键中的数据中拥有多个 select 和搜索框 - How can I have multiple select and search box from data in ASP.NET CORE foreign key 我无法从ASP.NET MVC将数据插入数据库中 - I can't insert data into my database from ASP.NET MVC 使用外键将ASP.NET EF Core插入多个表 - ASP.NET EF Core inserting into multiple tables with foreign key 显示来自不同数据库表的数据ASP.NET MVC - Display Data from Different Database Tables ASP.NET MVC 将数据插入包含asp.net中的外键的表的SQL查询 - SQL query to insert data into a table containing foreign key in asp.net 在同一页面上显示来自 ASP.NET MVC 5 中 2 个表的数据 - Display data from 2 tables in ASP.NET MVC 5 on the same page ASP.NET插入语句与外键约束冲突 - ASP.NET the insert statement conflicted with the foreign key constraint 如何在ASP.NET图表控件中使用外键表中的值 - How to use values from foreign key tables in asp.net chart control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM