I try save dataTable into SQL server. I can able to save records, but i try to save only three records But the records get duplicated(3 times) on SQL Table. I don't know why its get duplicated. Someone please help me to solve this issue ASAP
What I have tried:
Here is My code :
C#:
DataTable InfoTB = new DataTable("infotb");
DataColumn DC_Name = new DataColumn("name", typeof(string));
DataColumn DC_Age = new DataColumn("age", typeof(string));
DataColumn DC_Details = new DataColumn("details", typeof(string));
InfoTB.Columns.Add(DC_Name);
InfoTB.Columns.Add(DC_Age);
InfoTB.Columns.Add(DC_Details);
InfoTB.Rows.Add("Mano", "27", "Programmer");
InfoTB.Rows.Add("Jhon", "50", "Analyst");
InfoTB.Rows.Add("Diwani", "26", "Tester");
DataSet ds = new DataSet();
ds.Tables.Add(InfoTB);
SqlConnection sqlConn = new SqlConnection(localconstr);
SqlCommand ins_cmd = new SqlCommand("Insert_Info", sqlConn);
ins_cmd.CommandType = CommandType.StoredProcedure;
ins_cmd.Parameters.Add("@tblinfo", ds.Tables["infotb"]);
SqlHelper.UpdateDataset(ins_cmd, null, null, ds, "infotb");
Stored Procedure :
=================
CREATE PROCEDURE [dbo].[Insert_Info]
@tblinfo infoType READONLY
as
Begin
insert into infotb (name,age,details)
SELECT name,age,details FROM @tblinfo
End
UserDefinedTableType
=====================
CREATE TYPE [dbo].[infoType] AS TABLE(
[name] [varchar](50) NULL,
[age] [varchar](10) NULL,
[details] [varchar](250) NULL
)
GO
Table Script:
============
CREATE TABLE [dbo].[infotb](
[ID] [int] IDENTITY(1000,1) NOT NULL,
[Name] [varchar](50) NULL,
[Age] [varchar](50) NULL,
[Details] [varchar](150) NULL,
[RID] [uniqueidentifier] ROWGUIDCOL NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[infotb] ADD CONSTRAINT [DF_infotb_RID] DEFAULT (newid()) FOR [RID]
GO