[英]Pass RowID from application to a SQL Server stored procedure (or alternative)
I have a method in my application which is passing list<listitem>
into a stored procedure. 我的应用程序中有一个方法将
list<listitem>
传递到存储过程中。 I have created a table type data type to pass list<listitem>
. 我创建了一个表类型数据类型以传递
list<listitem>
。 Now I need to loop through in stored procedure to insert into another table. 现在,我需要遍历存储过程以插入到另一个表中。 For that purpose I created row id column in the table type which is generated automatically.
为此,我在自动生成的表类型中创建了行ID列。
Since the table type has 2 columns, it expects 2 parameters to pass from outside but I am generating it by identity column. 由于表类型具有2列,因此它期望从外部传递2个参数,但我是通过Identity列生成的。 Is there any way to avoid so that I don't pass value form outside?
有什么方法可以避免,这样我就不会在外面传递价值形式?
public void test(List<string> listItem) {
var table = new DataTable();
table.Columns.Add("col1", typeof(string));
foreach (string col1 in listItem) { table.Rows.Add(col1); }
SqlCommand cmd1 = new SqlCommand("TableTypeUpdateAnswers", conn);
cmd1.CommandType = CommandType.StoredProcedure;
SqlParameter sqlParam = cmd1.Parameters.AddWithValue("@tvpUpdateAnswers",table);
sqlParam.SqlDbType = SqlDbType.Structured ;
sqlParam.TypeName = "dbo.AnswerTableType";
conn.Open();
try
{ }
catch (Exception e)
{ }
}
Here is the SQL to create table type : 这是创建表类型的SQL:
CREATE TYPE [dbo].[AnswerTableType] AS TABLE(
RowID int not null primary key identity(1,1),
[col1] [nvarchar](50) NULL
)
And here is the stored procedure: 这是存储过程:
ALTER PROCEDURE [dbo].[TestTableType]
@TVPUPDATEANSWERS DBO.ANSWERTABLETYPE READONLY
AS
DECLARE
@CURRENTROW INT,
@VANSID int ,
@ROWSTOPROCESS INT
BEGIN
SET @CURRENTROW=0
SELECT L.col1 FROM @TVPUPDATEANSWERS L;
SET @ROWSTOPROCESS=@@ROWCOUNT
WHILE @CURRENTROW<@ROWSTOPROCESS
BEGIN
SET @CURRENTROW=@CURRENTROW+1
(SELECT @VANSID = col1
FROM @TVPUPDATEANSWERS
WHERE ROWID=@CURRENTROW);
//do something with table type datatype
INSERT INTO DBO.table1(col3,col4)
VALUES(@VANSID ,@col4);
END
END
看来您可能会从使用这种方法中受益-进行检查并让我知道... http://ilovedevelopment.blogspot.co.uk/2012/01/manage-multiple-updates-to-data-using- c.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.