我有一个返回新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();
答案 0 :(得分: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。您应该使用相同的实例来生成数字。
答案 1 :(得分: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()