简体   繁体   中英

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. As I have to take the record of class 10th, 12th, graduation, master. So I have created four rows for this... and 5 columns (year of exam, board, percentage, total mark, division).

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#)

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

 CREATE TYPE [dbo].[TypeName] AS TABLE(
[name1] [varchar](1000) NULL,
[name2] [varchar](1000) NULL,
[name3] [varchar](max) NULL
)
GO

Create Procedure in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM