我正在使用带有用户定义表类型的表值参数,下面是我的代码。我正在尝试从数据表填充存储过程。
更改过程 [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"};
//1×1添加89个列名 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();
}
}
最佳答案
您的 ACCOUNT
表和 CUSTOMER_1
具有引发问题的循环外键引用。类似的问题已经描述过here和 here 。
我建议你制作一个简化的数据库结构。据我所知,您不需要 ACCOUNT
表中的 CUST_NUM
列,您尝试使用 CUSTOMER_1
表中的外键。尽量保持数据库表结构尽可能简单,如我上面提供的第二个链接中所建议的那样。
关于c# - 如何获取存储过程和C#代码中的总记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50176430/