0

我有一个返回新 ID 的存储过程。

create proc [dbo].[WriteIT]
   (@deg nvarchar(max))
as
   DECLARE @s nvarchar(max)

   set @s = 'INSERT INTO test OUTPUT inserted.ID VALUES '
   set @s += @deg

   exec (@s)

如何将存储过程的多个结果放入一个int[]?

string con = "Server=.;Database=Adb;Trusted_Connection=True";

SqlConnection connet = new SqlConnection(con);

SqlCommand cm = new SqlCommand("WriteIT", connet);
cm.CommandType = System.Data.CommandType.StoredProcedure;    

bd = new StringBuilder();
string veri = "({0}),";
string sablon = "'{0}','{1}'";

for (int i = 0; i < 50; i++)
{
    bd.Append(string.Format(veri, string.Format(sablon, new Random().Next(0, 100000).ToString(), new Random().Next(0, 100000).ToString())));
}

cm.Parameters.AddWithValue("@deger", bd.ToString().Substring(0, bd.Length - 1));

if (connet.State == System.Data.ConnectionState.Closed)
    connet.Open();

string a = cm.ExecuteScalar();
4

2 回答 2

0

ExecuteScalar 旨在仅从结果中返回第一行的第一列。
如果您的 SProc 将返回多个值,则可以使用该ExecureReader()方法。

SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;

cmd.CommandText = "StoredProcedureName";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.

sqlConnection1.Close();


您还应该阅读Random class。您应该使用相同的实例来生成数字。

于 2013-09-11T03:25:19.797 回答
0
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Usp_CRMEntryInsert]
@custnam nvarchar(250),
@proj nvarchar(255),
@projid int,
@contact nvarchar(100),
@updfile nvarchar(150),
@vtype  nvarchar(100),
@sts varchar(15),
@recsts varchar(1),
@entryid int,
@entrydate datetime,
@dataentrydate datetime
AS
BEGIN
    SET NOCOUNT ON;
insert into CRMEntry (custnam,Proj,projid,contact,updfile,vtype,status,rec_status,entered_id,entered_date,entry_date) values (@custnam,@proj,@projid,@contact,@updfile,@vtype,@sts,@recsts,@entryid,@entrydate,@dataentrydate)
select scope_identity()

这个 Scope_identity() 返回最大值

maxid = cmd.ExecuteScalar()
于 2013-09-11T04:29:22.850 回答