[英]How to insert multiple rows of one user with common ID (primary key) into SQL Server database?
I want to take educational qualification records in my Asp.net form. 我想以我的Asp.net表格获取教育资格记录。 As I have to take the record of class 10th, 12th, graduation, master.
因为我要记录10年级,12年级,毕业,硕士的记录。 So I have created four rows for this... and 5 columns (year of exam, board, percentage, total mark, division).
因此,我为此创建了四行...和5列(考试年份,考试,考试分数,总成绩,除法)。
Now I want to insert all rows in database with one button click by maintaining the primary key common for one user in all four records. 现在,我想通过一次单击来插入数据库中的所有行,方法是保持所有四个记录中一个用户的通用主键。
Please help me with the code (C#) 请帮我提供代码(C#)
You may want to look at using a Composite Primary Key . 您可能想看看使用复合主键 。 This is a Primary Key that uses multiple columns to compose a single key.
这是一个主键,它使用多列组成一个键。 There are arguments for and against this strategy.
有支持和反对这种策略的论点。 See: What are the pros and cons of using multi column primary keys?
请参阅: 使用多列主键的优缺点是什么?
As an example, if your table looks like this: 例如,如果您的表如下所示:
CREATE TABLE [dbo].[StudentExam]
(
[StudentId] INT NOT NULL PRIMARY KEY,
[Year] INT NOT NULL,
[Board] SOMEDATATYPE NOT NULL,
[Percentage] FLOAT NOT NULL,
[TotalMark] INT NOT NULL,
[Division] SOMEDATATYPE NOT NULL,
)
You can alter the schema to look like this instead: 您可以更改架构,使其看起来像这样:
CREATE TABLE [dbo].[StudentExam]
(
[StudentId] INT NOT NULL,
[Year] INT NOT NULL,
[Board] SOMEDATATYPE NOT NULL,
[Percentage] FLOAT NOT NULL,
[TotalMark] INT NOT NULL,
[Division] SOMEDATATYPE NOT NULL,
CONSTRAINT [PK_StudentExam] PRIMARY KEY ([StudentId], [Year])
)
By doing this, you are declaring that for any given row in this table, it is uniquely identified by the Student and Year together. 这样,您就声明此表中的任何给定行均由“学生”和“年份”共同唯一标识。 You can still query on just the student, or just the year, but only together will they identify a row.
您仍然可以仅查询学生,也可以查询年份,但是只有它们一起才能确定一行。
For more information on primary keys, see Create Primary Keys 有关主键的更多信息,请参见创建主键。
Create Table Type in Sql 在Sql中创建表类型
CREATE TYPE [dbo].[TypeName] AS TABLE(
[name1] [varchar](1000) NULL,
[name2] [varchar](1000) NULL,
[name3] [varchar](max) NULL
)
GO
Create Procedure in SQL : 在SQL中创建过程:
ALTER PROCEDURE [dbo].[InsertData]
@TableType TypeName readonly
AS
INSERT INTO [dbo].[Table_Master]
(Tname1,Tname2,Tname3)
select name1,name2,name3 from @TableType
Then Go To Code Behind 然后转到隐藏代码
OpenConnection();
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = spName;
sqlcmd.Connection = sqlconn;
SqlParameter tvpParam = sqlcmd.Parameters.AddWithValue("@Type", Your
Datatable);
tvpParam.SqlDbType = SqlDbType.Structured;
SqlParameter returnParameter = sqlcmd.Parameters.Add("RetVal",
SqlDbType.Int);
returnParameter.Direction = ParameterDirection.ReturnValue;
sqlcmd.ExecuteNonQuery();
int Result = (int)returnParameter.Value;
sqlcmd.Dispose();
return Result;
Pass your DT in Uper Code...It Will Work Completely 通过Uper Code传递您的DT ...它将完全起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.