我正在使用带有用户定义的表类型的表值参数,这是我的代码。我试图从我的数据表中填充我的存储过程。
ALTER PROCEDURE [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"};
//逐个添加89个列名 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();
}
}
答案 0 :(得分:0)
您可能在创建数据库之前尝试添加外键。 尝试创建它们,然后在添加外键后更新它们。
[编辑]
下面:
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_1(CUST_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_1(CUST_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);
答案 1 :(得分:0)