I am using a table valued parameter with a user defined table type here is my code below. I am trying to populate my stored procedure from my data table.
ALTER PROCEDURE [dbo].[TableName] @dt AS dbo.DataTableAsType READONLY
AS
BEGIN
INSERT INTO dbo.[DataTableAsType]
([Column names]) --There are 89 column names
SELECT
([ColumnNames])
FROM @dt
END
Second Stored procedure
@totalRecords int OUTPUT
INSERT INTO dbo.tablename1 FROM dbo.tablename2
SELECT @totalRecords = COUNT(*) FROM dbo.[tableName2]
public void InsertDataTableAF2CSV(string ext)
{
DataTable tvp = new DataTable();
string[] arr = new string[89] {"names go here"};
//add 89 column names 1 by 1 tvp.Columns.Add(new DataColumn("column name", typeof(string)));
//populate datarows I currently have over 1,000 rows.
DataRow row;
for (int i = 0; i <= arr.Length; i++)
{
row = tvp.NewRow();
row["Column name"] = i;
//I add all 89 column names = i then I add rows.
tvp.Rows.Add(row);
}
//read the file name I entered
tvp= ReadFile(filename, " ", null);
//Passing a Table-Valued Parameter to a Stored Procedure
using (SqlConnection con = new SqlConnection(connection name))
{
connection.Open();
//Execute the cmd
// Configure the command and parameter.
SqlCommand cmd = new SqlCommand("dbo.storedprocedure", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 5000;
///SqlParameter tvparam = cmd.Parameters.AddWithValue("@dt", tvp);
// Create a DataTable with the modified rows.
DataTable addedCategories = tvp.GetChanges(DataRowState.Added);
// these next lines are important to map the C# DataTable object to the correct SQL User Defined Type
SqlParameter parameter = new SqlParameter("@dt", SqlDbType.Structured)
{
//TypeName = "dbo.DataTableAsType",
TypeName = "dbo.importDataTable",
Value = tvp
};
cmd.ExecuteNonQuery();
con.Close();
}
}