H
harifajri
Hi,
I'm using C# and using objects: SqlDataAdapter, SqlCommand and
SQLCommandBuilder..
I have problem while inserting value to identity column on database
(MSSQLserver)..
for example, below is my table structure:
CREATE TABLE dbo.Tool(
ID INT IDENTITY NOT NULL PRIMARY KEY,
Name VARCHAR(40) NOT NULL
)
-- Then i insert values into tool table.
INSERT INTO dbo.Tool(Name) VALUES ('Screwdriver')
INSERT INTO dbo.Tool(Name) VALUES ('Hammer')
INSERT INTO dbo.Tool(Name) VALUES ('Saw')
-- Then i Create a gap in the identity values, I delete 'Hammer'
DELETE dbo.Tool WHERE Name = 'Hammer'
-- now i want to insert explicit ID, to avoid warning, i add statement
like this
SET IDENTITY_INSERT dbo.Tool ON
-- then i insert explicit ID to database..
INSERT INTO dbo.Tool (ID, Name) VALUES (3, 'Garden shovel')
i can successfully insert explicit ID using Sql Statement..
my problem comes when i try to create C# code like Sql Statements
above..
Now, see my code bolow:
string ConnectionStringToDB = "...";
SqlCommand cmd = new SqlCommand();
cmd.Connection = new SqlConnection(ConnStringToDB);
cmd.CommandType = CommandType.Text;
cmd.CommandText= "select * from Tool";
SqlDataAdapter sda = new SqlDataAdapter(cmd);
SqlCommandBuilder builder = new SqlCommandBuilder(sda);
DataTable dtTarget = new DataTable();
sda.Fill(dtTarget);
// Code to modify dtTarget
....
Now, this is the problem:
i want to insert explicit ID on identity column using C#..
i add a new DataRow on DtTarget,
but when i do ...
sda.Fill(dtTarget); // <-- error...
How to add statement "SET IDENTITY_INSERT dbo.Tool ON" on
SqlDataAdapter ?
Regards
HF
I'm using C# and using objects: SqlDataAdapter, SqlCommand and
SQLCommandBuilder..
I have problem while inserting value to identity column on database
(MSSQLserver)..
for example, below is my table structure:
CREATE TABLE dbo.Tool(
ID INT IDENTITY NOT NULL PRIMARY KEY,
Name VARCHAR(40) NOT NULL
)
-- Then i insert values into tool table.
INSERT INTO dbo.Tool(Name) VALUES ('Screwdriver')
INSERT INTO dbo.Tool(Name) VALUES ('Hammer')
INSERT INTO dbo.Tool(Name) VALUES ('Saw')
-- Then i Create a gap in the identity values, I delete 'Hammer'
DELETE dbo.Tool WHERE Name = 'Hammer'
-- now i want to insert explicit ID, to avoid warning, i add statement
like this
SET IDENTITY_INSERT dbo.Tool ON
-- then i insert explicit ID to database..
INSERT INTO dbo.Tool (ID, Name) VALUES (3, 'Garden shovel')
i can successfully insert explicit ID using Sql Statement..
my problem comes when i try to create C# code like Sql Statements
above..
Now, see my code bolow:
string ConnectionStringToDB = "...";
SqlCommand cmd = new SqlCommand();
cmd.Connection = new SqlConnection(ConnStringToDB);
cmd.CommandType = CommandType.Text;
cmd.CommandText= "select * from Tool";
SqlDataAdapter sda = new SqlDataAdapter(cmd);
SqlCommandBuilder builder = new SqlCommandBuilder(sda);
DataTable dtTarget = new DataTable();
sda.Fill(dtTarget);
// Code to modify dtTarget
....
Now, this is the problem:
i want to insert explicit ID on identity column using C#..
i add a new DataRow on DtTarget,
but when i do ...
sda.Fill(dtTarget); // <-- error...
How to add statement "SET IDENTITY_INSERT dbo.Tool ON" on
SqlDataAdapter ?
Regards
HF