尝试执行SP然后在文本框中生成结果

时间:2011-10-21 18:10:03

标签: asp.net stored-procedures c#-4.0

我的SP在列调用userID中显示单个userID(基于前3个字符.pre =前缀),将临时表与现有表进行比较。

CREATE PROCEDURE [dbo].[getUserId]
@pre char(3)
AS

create table #tmp1 (userID char(7)) 
declare @fmt char(4) = null
  , @num int = null
  , @numEnd int = null  
 select @pre from activeIds
 select @fmt='0000'
 , @num = 0 --change the # value of the ID
 , @numEnd = 10000 --end at 9999

  while @num < @numEnd
  begin
        insert into #tmp1 (userId) values (@pre + substring(@fmt, 1, len(@fmt)-len(@num)) 
        + cast(@num as char))  --insert the prefix, cast(convert) @num to char
        set @num = @num + 1
  end

SELECT top 1 a.userId  --display all IDs not used
FROM #tmp1 AS a
WHERE not EXISTS
(SELECT * 
FROM activeIds AS b
WHERE a.userId = b.samAccountName)
and a.userId like (@pre + '%')
ORDER BY a.userId ASC ;
GO

在C#中,这就是我所拥有的,我现在非常坚持......

 protected void btnGenID_click(object sender, EventArgs e)
 {
        string Yre = "Yre";
        string sql = string.Format("exec getUserId @pre=N'{0}'",  Yre);
        //_dt is DataTable and _oDAW is a connection string.
        _dt = _oDAW.GetDataTable(sql); 
        FillUserData(); 
 }
 protected void FillUserData()
 {
        // fill in the textbox. 2nd [] picks a non temp column 
        // but trying to get SP output.
        PrefixBox.Text = Convert.ToString(_dt.Rows[0]["userId"]).Trim();
 }

在ASP中

 <asp:TextBox runat="server" ID="PrefixBox" MaxLength="7"></asp:TextBox>

0 个答案:

没有答案