简体   繁体   English

C# 和 T-SQL 将用户定义的表插入另一个表不起作用

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

I plan to send a data table to the SQL Server database, and INSERT the new rows.我计划将数据表发送到 SQL Server 数据库,并INSERT新行。 There will be multiple insert posts for each row in the program.程序中的每一行都会有多个插入帖子。 No errors are returned but the rows aren't added to the table.不会返回任何错误,但不会将行添加到表中。 The product table has got more columns than [dbo].[CSV_ADDProducts] but I have selected the columns it would map to.产品表的列比[dbo].[CSV_ADDProducts]但我选择了它要映射到的列。

Can anyone spot the mistake I'm making or suggest a better way to do this?谁能发现我犯的错误或提出更好的方法来做到这一点? Thanks谢谢

SQL Server stored procedure SQL Server 存储过程

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# program 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);

User defined 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

When I run this, it does work as a query on the DBMS当我运行它时,它确实可以作为对 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

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

the commented out code is what was not working.注释掉的代码不起作用。 command.Parameters.AddWithValue("@uploadedTable", DT); works fine工作正常

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; }
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM