【发布时间】:2018-10-15 00:43:21
【问题描述】:
我正在使用具有用户定义表类型的表值参数,下面是我的代码。我正在尝试从我的数据表中填充我的存储过程。
ALTER PROCEDURE [dbo].[TableName] @dt AS dbo.DataTableAsType 只读
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"};
//将 89 个列名 1 乘 1 tvp.Columns.Add(new DataColumn("列名", 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();
}
}
【问题讨论】:
标签: c# mysql asp.net sql-server asp.net-mvc