C# 和 T-SQL Insert User Defined Table into a another table 不工作

C# and T-SQL Insert User Defined Table into a another table not working

我计划将数据 table 发送到 SQL 服务器数据库,并 INSERT 新行。程序中的每一行都会有多个插入帖子。未返回任何错误,但行未添加到 table。产品 table 的列数比 [dbo].[CSV_ADDProducts] 多,但我选择了它将映射到的列数。

任何人都可以发现我犯的错误或建议更好的方法吗?谢谢

SQL 服务器存储过程

CREATE PROCEDURE [dbo].[spUpload_AddBulkProducts]
 @uploadedTable [dbo].[CSV_ADDProducts] readonly
 AS
BEGIN TRY
INSERT INTO [dbo].[Products]
([Product Item],[Product SKU],[Product Name],[Product Active],[Product Selling Price],[Product Description],[Product Purchase Description],[Product VAT Code ID],[Product Last Update])
SELECT
[Product Item],[Product SKU],[Product Name],[Product Active],[Product Selling Price],[Product Description],[Product Purchase Description],[Product VAT Code ID],[Product Last Update]
FROM @uploadedTable
END TRY
BEGIN CATCH
END CATCH

C#程序

DataTable POSTCSVCREATE = new DataTable();
        POSTCSVCREATE.Columns.Add("Product Item", typeof(SqlString));
        POSTCSVCREATE.Columns.Add("Product SKU", typeof(SqlInt64));
        POSTCSVCREATE.Columns.Add("Product Name", typeof(SqlString));
        POSTCSVCREATE.Columns.Add("Product Active", typeof(SqlString));
        POSTCSVCREATE.Columns.Add("Product Selling Price", typeof(SqlMoney));
        POSTCSVCREATE.Columns.Add("Product Description", typeof(SqlString));
        POSTCSVCREATE.Columns.Add("Product Purchase Description", typeof(SqlString));
        POSTCSVCREATE.Columns.Add("Product VAT Code ID", typeof(SqlInt64));
        POSTCSVCREATE.Columns.Add("Product Last Update", typeof(SqlDateTime));
        
        foreach (ParseQBProduct.Product i in create)
        {
            var rows = VAT.Tables[0].Select();
            string VATID = "1";
            foreach (DataRow row in rows)
            {
                if (row["VAT Name"].ToString() == i.VAT.ToString())
                {
                    VATID = row["VAT ID"].ToString();
                }
            }
            string dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");

            if (i.ProductSellingPrice.Trim() != "")
            {
                POSTCSVCREATE.Rows.Add(i.UniqueTitle, Convert.ToInt64(i.manunum), i.name, i.ProductActive, (SqlMoney)Convert.ToDecimal(i.ProductSellingPrice), i.ProductDescription, i.ProductPurchaseDescription, Convert.ToInt64(VATID), (SqlDateTime)Convert.ToDateTime(dt));
                recentAddedfile.Add(i.manunum+" -- " +i.UniqueTitle);
            }
           
        }
        Authentication.LoginUC.instance.Session.Procedure_UploadTable("spUpload_AddBulkProducts", POSTCSVCREATE);

用户定义Table

CREATE TYPE [dbo].[CSV_ADDProducts] AS TABLE(
    [Product Item] [nvarchar](max) NULL,
    [Product SKU] [bigint] NULL,
    [Product Name] [nvarchar](max) NULL,
    [Product Active] [nchar](10) NULL,
    [Product Selling Price] [money] NULL,
    [Product Description] [nvarchar](max) NULL,
    [Product Purchase Description] [nvarchar](max) NULL,
    [Product VAT Code ID] [bigint] NOT NULL,
    [Product Last Update] [datetime] NULL
)
GO

当我 运行 这样做时,它确实作为 DBMS 上的查询工作

DECLARE @Table AS [dbo].[CSV_ADDProducts]
Insert into @Table
Values ('test',1,'test name','true',10.44,'des','pdes',2,'2020-09-10 12:12:12.000')
EXECUTE spUpload_AddBulkProducts @Table

我的来源:https://www.sqlshack.com/table-valued-parameters-in-sql-server/

注释掉的代码是不起作用的。 command.Parameters.AddWithValue("@uploadedTable", DT); 工作正常

public bool Procedure_UploadTable(string procedure,DataTable DT)
            {
                DBConn.Open();
                using (var command = new SqlCommand(procedure) { CommandType = CommandType.StoredProcedure })
                {
                    command.Connection = DBConn;
                    command.Parameters.AddWithValue("@uploadedTable", DT);
                    //SqlParameter param = new SqlParameter("@uploadedTable", SqlDbType.Structured)
                    //{
                    //    TypeName = "[dbo].[TBLCSV_ADDProducts]",
                    //    Value = DT
                    //};
                    try
                    {
                        command.ExecuteNonQuery();
                        DBConn.Close();
                        return true;
                    }
                    catch(Exception e) {
                        DBConn.Close();
                        MessageBox.Show(e.Message);
                        return false; }
                }
            }