如何在存储过程和c代码中获取总记录?

qaxu7uf2  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(273)

我正在使用一个带有用户定义表类型的表值参数,下面是我的代码。我正在尝试从数据表填充存储过程。
将过程[dbo].[tablename]@dt改为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"};

//添加89个列名1乘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();
        }
    }
zed5wv10

zed5wv101#

可能您正在尝试在创建数据库之前添加外键。尝试创建它们,然后在更新它们之后添加外键。
[编辑]
在这里:

CREATE TABLE ACCOUNT
(ACCT_ID CHAR(10),
BRANCH_ID CHAR(20),
CUST_NUM CHAR(15),
ACCT_TYPE CHAR(20),
FOREIGN KEY (BRANCH_ID) REFERENCES BRANCH_1 (BRANCH_ID),
FOREIGN KEY (CUST_NUM) REFERENCES CUSTOMER_1 (CUST_NUM));

您正在尝试引用尚不存在的customer\u 1(cust\u num)。
[编辑2]
似乎缺少一些索引。试试这个:
可能您正在尝试在创建数据库之前添加外键。尝试创建它们,然后在更新它们之后添加外键。
[编辑]
在这里:

CREATE TABLE ACCOUNT
(ACCT_ID CHAR(10),
BRANCH_ID CHAR(20),
CUST_NUM CHAR(15),
ACCT_TYPE CHAR(20),
FOREIGN KEY (BRANCH_ID) REFERENCES BRANCH_1 (BRANCH_ID),
FOREIGN KEY (CUST_NUM) REFERENCES CUSTOMER_1 (CUST_NUM));

您正在尝试引用尚不存在的customer\u 1(cust\u num)。
[编辑2]
试试这个:

CREATE TABLE BRANCH_1
    (BRANCH_ID CHAR(20) PRIMARY KEY,
    BRANCH_NAME CHAR(20),
    BRANCH_ADDRESS CHAR(30));

CREATE TABLE EMPLOYEE
    (EMP_ID CHAR(4) PRIMARY KEY,
    EMP_FNAME CHAR(50),
    EMP_LNAME CHAR(50),
    BRANCH_ID CHAR(20));

    CREATE TABLE ACCOUNT
    (ACCT_ID CHAR(10),
    BRANCH_ID CHAR(20),
    CUST_NUM CHAR(15),
    ACCT_TYPE CHAR(20));

CREATE TABLE CUSTOMER_1
    (CUST_NUM CHAR(15),
    CUS_FNAME CHAR(50),
    CUS_LNAME CHAR(50),
    CUS_ADDRESS CHAR(15),
    BRANCH_ID CHAR(20),
    ACCT_ID CHAR(10),
    LOAN_ID CHAR(10));

CREATE TABLE LOAN
    (LOAN_ID CHAR(10),
    LOAN_NAME CHAR(50),
    LOAN_TYPE CHAR(20),
    CUST_NUM CHAR(15));

ALTER TABLE CUSTOMER_1 ADD INDEX(CUST_NUM);
ALTER TABLE ACCOUNT ADD INDEX(ACCT_ID);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY (BRANCH_ID) REFERENCES BRANCH_1 (BRANCH_ID);
ALTER TABLE ACCOUNT ADD FOREIGN KEY (BRANCH_ID) REFERENCES BRANCH_1 (BRANCH_ID);
ALTER TABLE ACCOUNT ADD FOREIGN KEY (CUST_NUM) REFERENCES CUSTOMER_1 (CUST_NUM);
ALTER TABLE CUSTOMER_1 ADD FOREIGN KEY (BRANCH_ID) REFERENCES BRANCH_1 (BRANCH_ID);
ALTER TABLE CUSTOMER_1 ADD FOREIGN KEY (ACCT_ID) REFERENCES ACCOUNT (ACCT_ID);
ALTER TABLE LOAN ADD FOREIGN KEY (CUST_NUM) REFERENCES CUSTOMER_1 (CUST_NUM);
6tdlim6h

6tdlim6h2#

你的 ACCOUNT table和table CUSTOMER_1 有一个循环外键引用引发了问题。这里和这里已经描述了类似的问题。
我建议你做一个简化的数据库结构。据我所知,你不需要 CUST_NUM 列中的 ACCOUNT 您正试图从中使用外键的表 CUSTOMER_1 table。尽量保持数据库表结构尽可能简单,正如我在上面提供的第二个链接中所建议的那样。

相关问题