【发布时间】:2015-08-16 16:49:19
【问题描述】:
我是初学者 asp.net 程序员,我的项目是在线购物课程 我有一些问题
我有 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
我有三页,在第一页中,我将一些数据传递到另一页,在第二页中,我将数据设置到我的数据库中。
首页代码
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");
}
}
第二页代码:
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");
}
}
我的问题是我不知道如何将值插入到具有外键和主键的表中。cmd1 和 cmd2 工作正常,但我无法编写一些东西来设置他们有的 order 表和 orderdetails 表 另一个表的外键...
【问题讨论】:
-
您面临的问题是什么,使用外键将数据插入表中,您只需要编写类似的插入代码,如果违反外键引用,它将抛出您需要处理的异常。
-
我的问题是我需要外键与其引用具有相同的值,换句话说我需要一个客户的所有信息之间的关系......
标签: c# sql asp.net sql-server