salvationishere salvationishere - 1 year ago 115
SQL Question

How to execute Table SP in C#

I looked at the SQLTeam website but now am having a new problem cause I have an IDENTITY column and their example does not.

I have SQL Server 2008 and VS 2008. I am trying to execute the InsertPIF SP using C# and a table UDT, but am getting an exception. I have looked at a SQLTeam website
example with a Table UDT, but their example doesn't have an identity column like mine does.

However, their example table doesn't have an identity column like mine.
What I am trying to do should be simple. I have tried many different variations of this, but essentially:

ALTER PROCEDURE [dbo].[InsertPIF] @InputFileParam parseInputFile READONLY
INSERT dbo.ParentTable SELECT strRow FROM @InputFileParam


CREATE TABLE [dbo].[ParentTable](
[ParentID] [int] IDENTITY(1,1) NOT NULL,
[strInput] [varchar](8000) NULL,

and type:

CREATE TYPE [dbo].[parseInputFile] AS TABLE(
[NumCols] [int] IDENTITY(1,1) NOT NULL,
[strRow] [varchar](500) NOT NULL,

What I am trying to do is to execute the above SP to insert new records into the ParentTable. Example:

INSERT INTO ParentTable(strInput) VALUES ('A3|BB|C|DDD')
INSERT INTO ParentTable(strInput) VALUES ('A5|Hello|My|Darling')

Here is more of my C# code prior to the AppendData section:

DataTable dt = new DataTable();
String[] header = myStringArray[0].Split(’|');

DataRow dr = dt.NewRow();
DataColumn dc = new DataColumn();
dc.DataType = typeof(Int32);
dc.ColumnName = “NumCols”;

DataColumn dc2 = new DataColumn();
dc2.DataType = typeof(string);
dc2.ColumnName = “strRow”;
dr["NumCols"] = 1;
dr["strRow"] = "AA|BBB|CC|DD|E";

SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "dbo.InsertPIF";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
SqlParameter param = cmd.Parameters.AddWithValue("@InputFileParam", SqlDbType.Structured);

param.Value = dt;
RowsAffected = cmd.ExecuteNonQuery();

I set identity to be turned on for the ParentTable, but it seems like I have to execute this each session, so I'm not sure how to remove identity requirement or else to make
identity remain on. Exception:

INSERT into an identity column not allowed on table variables. The data for table-valued parameter '@InputFileParam' doesn't conform to the table type of the parameter.

How do I execute this SP?

Answer Source

I think Roland is on the right track - the IDENTITY columns definitely are the problem. Since your ultimate target table already has an IDENTITY column which will be filled automagically for you, I would recommend not specifying the same column in your user-defined table type (and also do not declare a primary key on the table type):

CREATE TYPE [dbo].[parseInputFile] AS TABLE
    ([strRow] [varchar](500) NOT NULL)

Then in your C# code, just set up a DataTable with a single column:

DataTable dt = new DataTable();

DataColumn dc = new DataColumn();
dc.DataType = typeof(string);
dc.ColumnName = “strRow”;

and then add your columns to your DataTable "dt" - with just the one string "strRow".

In your stored proc that then gets called with your user-defined table type, just simply insert your strings:

ALTER PROCEDURE [dbo].[InsertPIF] @InputFileParam parseInputFile READONLY
  INSERT INTO dbo.ParentTable(strInput)
    SELECT strRow FROM @InputFileParam

That should do it, I hope! (haven't had the time to test this myself)


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download