0

我被迫创建一个使用两个数据表的过程,例如:

ALTER PROCEDURE [sesuser].[Login_GetUserList]
(
    @beginsWith NVARCHAR(20) = NULL,
    @SortBy nvarchar(20) = NULL,
    @sortByDirection nvarchar(5) = N'ASC'
)
AS
BEGIN

    DECLARE @searchStr NVARCHAR(21)

    IF @beginsWith IS NULL
        SET @beginsWith = N''
    ELSE
        SET @beginsWith = dbo.SuperTrim(@beginsWith);

    IF LEN(@beginsWith) > 0
        SET @searchStr = @beginsWith + N'%'
    ELSE
        SET @searchStr = N'%'

DECLARE @sqlSTR NVARCHAR(4000)
DECLARE @sqlOffice NVARCHAR(100)
DECLARE @sqlOfficeID NVARCHAR(10)
DECLARE @sqlEndSTR NVARCHAR(4000)

SET @sqlSTR = N' SELECT 
        [SESLoginID] AS LoginID
        ,[SESSuspended] AS LoginSuspended
        ,[SESAdmin] AS Role_Admin
        ,[SESLoginID]
        ,[SESFirstName]
        ,[SESLastName]
        ,[SESEmail]
        ,[SESSuspended]
        FROM sesuser.SESLogin
        WHERE SESFirstName LIKE ''' + @searchStr + ''''

SET @sqlOfficeID = N' SELECT 
        [SESLoginID]
        FROM sesuser.SESLogin
        WHERE SESFirstName LIKE ''' + @searchStr + ''''

SET @sqlOffice = N' SELECT
        [OfficeName]
        FROM sesuser.Office
        WHERE OfficeID LIKE ''' + @sqlOfficeID + ''''

SET @sqlEndSTR = @sqlSTR + @sqlOffice

PRINT @sqlEndSTR

EXEC sp_ExecuteSQL @sqlEndSTR

END

据我了解,此代码应该在办公室 ID 和等效办公室名称表中,以用另一个表中的名称替换办公室 ID 并返回它。

然后我在我的 c# 代码中的一个方法中使用检索数据字符串:

public static DataTable GetUserList(string beginsWith, out string errMessage, string sortBy, string sortByDirection)
{
   DataTable dt = null;
   int errNum = 0;
   string sql = "[sesuser].[Login_GetUserList]";
   SqlDataAdapter adap = null;

   SqlParameter param = null;

   errMessage = "";
   SqlConnection conn = Functions.Database.DBConnect(out errNum);

   try
   {
      SqlCommand cmd = new SqlCommand(sql);
      cmd.Connection = conn;
      cmd.CommandType = CommandType.StoredProcedure;

      param = new SqlParameter("@beginsWith", SqlDbType.NVarChar, 40);
      param.Value = string.IsNullOrEmpty(beginsWith) ? "" : beginsWith.Trim();
      cmd.Parameters.Add(param);
      cmd.Parameters.AddWithValue("@SortBy", sortBy);
      cmd.Parameters.AddWithValue("@sortByDirection", sortByDirection);

      adap = new SqlDataAdapter(cmd);

      dt = new DataTable();
      adap.Fill(dt);
   }
   catch (Exception ex)
   {
      errMessage = ex.Message;
   }
   finally
   {
      Functions.Database.DBClose(conn);
   }

   return dt;
}

后来在我的 asp.net 代码中,我回拨办公室名称

<asp:BoundField DataField="OfficeName" HeaderText="Business Unit" ReadOnly="True"  />

问题是,它出错,说它找不到OfficeName

有什么我忘记了吗?还是我做错了?

4

2 回答 2

1

看起来您的存储过程返回两个子集。

像这样修改您的存储过程:

ALTER PROCEDURE [sesuser].[Login_GetUserList]
(   @beginsWith NVARCHAR(20) = NULL,
    @SortBy nvarchar(20) = NULL,
    @sortByDirection nvarchar(5) = N'ASC')
AS
BEGIN    
    DECLARE @searchStr NVARCHAR(21)    
    IF @beginsWith IS NULL
        SET @beginsWith = N''
    ELSE
        SET @beginsWith = dbo.SuperTrim(@beginsWith);    
    IF LEN(@beginsWith) > 0
        SET @searchStr = @beginsWith + N'%'
    ELSE
        SET @searchStr = N'%'    
DECLARE @sqlSTR NVARCHAR(4000)    
SET @sqlSTR = N' SELECT 
         [OfficeName]
        ,[SESLoginID] AS LoginID
        ,[SESSuspended] AS LoginSuspended
        ,[SESAdmin] AS Role_Admin
        ,[SESLoginID]
        ,[SESFirstName]
        ,[SESLastName]
        ,[SESEmail]
        ,[SESSuspended]
        FROM sesuser.SESLogin
        INNER JOIN sesuser.Office
        ON sesuser.Office.OfficeID = sesuser.SESLogin.SESLoginID
        WHERE SESFirstName LIKE ''' + @searchStr + ''''    
PRINT @sqlSTR    
EXEC sp_ExecuteSQL @sqlSTR    
END
于 2011-06-23T08:53:16.277 回答
0

我不明白为什么查询在查询字符串中,肯定会更好

ALTER PROCEDURE [sesuser].[Login_GetUserList]
(  
@beginsWith NVARCHAR(20) = NULL,
@SortBy nvarchar(20) = NULL,
@sortByDirection nvarchar(5) = N'ASC'
)
AS
BEGIN    
DECLARE @searchStr NVARCHAR(21)    
IF @beginsWith IS NULL
    SET @beginsWith = N''
ELSE
    SET @beginsWith = dbo.SuperTrim(@beginsWith);    
IF LEN(@beginsWith) > 0
    SET @searchStr = @beginsWith + N'%'
ELSE
    SET @searchStr = N'%'    

SELECT  [OfficeName]
    ,[SESLoginID] AS LoginID
    ,[SESSuspended] AS LoginSuspended
    ,[SESAdmin] AS Role_Admin
    ,[SESLoginID]
    ,[SESFirstName]
    ,[SESLastName]
    ,[SESEmail]
    ,[SESSuspended]
    FROM sesuser.SESLogin
    INNER JOIN sesuser.Office
    ON sesuser.Office.OfficeID = sesuser.SESLogin.SESLoginID
    WHERE SESFirstName LIKE @searchStr 

END
于 2011-06-23T11:08:35.033 回答