简体   繁体   English

如何将一个具有公共ID(主键)的用户的多行插入SQL Server数据库?

[英]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.

相关问题 数据库表(重新)设计。 将多行合并为一个并分配主键。 Microsoft SQL服务器 - Database table (re)design. Combine multiple rows into one and assign primary key. Microsoft SQL Server 如何在C#中使用一个命令将条目插入SQL Server数据库并返回包含主键标识的已插入列? - How do I insert an entry into a SQL Server database and return inserted columns including primary key identity with one command in C#? 主键列已设置默认标识(1, 1)时如何向sql server数据库插入数据 - How to insert data into sql server database when the primary key column already set default identity(1, 1) 如何获取插入数据库中的序列/触发器主键并将其插入到我的类参数ID C#中 - How to get the sequence/trigger primary key that is inserted into the database and insert it into my class parameter id C# sql-server主键(ID)不能正确递增 - sql-server primary key (ID) does not increment correctly 如何使用LINQ-TO-SQL基于非主键字段项的集合删除多行? - How to delete multiple rows based on a collection of non-primary key field items using LINQ-TO-SQL? 如何将数据插入涉及C#中主键和外键的sql server关系数据库 - How to insert data to a sql server relational database that involve primary and foreign keys in C# 循环遍历temp目录中的多个文件,并将文件插入到具有现有行/ ID的MS SQL数据库中 - Loop through multiple files in temp directory and insert files into MS SQL database with existing rows/id's SQL插入一行或多行数据? - SQL Insert one row or multiple rows data? 使用参数Sql Server插入多行 - Insert multiple rows with parameters Sql Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM