使用ado.net

时间:2018-04-02 17:19:55

标签: c# sql-server ado.net

当我尝试使用ado.net运行存储过程时,我得到了一些非常奇怪的结果。我已使用EXEC dbo.AddressSearch验证存储过程是否正确执行。

public DataTable ExecuteStoredProcedure(string name, params SqlParameter[] parameters)
{
        var dataTable = new DataTable();
        var mode = _configurationService.AppSetting("mode");
        var connectionString = _configurationService.ConnectionString($"{mode}ConnectionString");

        using (var sqlConnection = new SqlConnection(connectionString))
        {
            sqlConnection.Open();

            using (var command = new SqlCommand(name,sqlConnection))
            {
                foreach (var parameter in parameters)
                {
                    command.Parameters.AddWithValue(parameter.ParameterName, parameter.Value);
                }

                using (var dataAdapter = new SqlDataAdapter(command))
                {
                    dataAdapter.Fill(dataTable);
                }
            }
        }

        return dataTable;
}

以下是调用存储过程的代码;我已经验证它正在使用SQL Server Profiler调用存储过程。以下是它发送的内容:

exec sp_executesql 
N'AddressSearch',
N'@PageSize int,
@Start int,
@StreetName nvarchar(4000),
@Notes nvarchar(4000),
@ParcelNumber nvarchar(4000),
@ZoningCode nvarchar(4000),
@HouseNumber nvarchar(2),
@County nvarchar(4000),
@Municipality nvarchar(4000)
,@SubDivision nvarchar(4000)',
@PageSize=50,
@Start=1,
@StreetName=N'',
@Notes=N'',
@ParcelNumber=N'',
@ZoningCode=N'',
@HouseNumber=N'12',
@County=N'',
@Municipality=N'',
@SubDivision=N''

所以我用SSMS调试了这个,我注意到没有设置变量。谁能告诉我为什么会这样呢?

Json被派遣。

{
    "county":"",
    "esn":"",
    "houseNumber":"12",
    "municipality":"",
    "notes":"",
    "page":"1",
    "pageSize":"50",
    "parcelNumber":"",
    "start":"0",
    "streetName":"",
    "subDivision":"",
    "uniqueId":"",
    "zoningCode":""
}

填充所有json:

{
    "county":"a",
    "esn":"12",
    "houseNumber":"12",
    "municipality":"",
    "notes":"notes",
    "page":"1",
    "pageSize":"50",
    "parcelNumber":"abd",
    "start":"0",
    "streetName":"3",
    "subDivision":"sub",
    "uniqueId":"1",
    "zoningCode":"15"
}

发送到SQL Server:

exec sp_executesql N'AddressSearch',N'@PageSize int,@Start int,@StreetName nvarchar(1),@Notes nvarchar(5),@ParcelNumber nvarchar(3),@ZoningCode nvarchar(2),@HouseNumber nvarchar(2),@County nvarchar(1),@Municipality nvarchar(4000),@SubDivision nvarchar(3),@Esn int,@UniqueId int',@PageSize=50,@Start=0,@StreetName=N'3',@Notes=N'notes',@ParcelNumber=N'abd',@ZoningCode=N'15',@HouseNumber=N'12',@County=N'a',@Municipality=N'',@SubDivision=N'sub',@Esn=12,@UniqueId=1

存储过程:

IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID(N'AddressSearch') AND TYPE IN (N'P',N'PC'))
    DROP PROCEDURE dbo.AddressSearch
GO

CREATE PROCEDURE dbo.AddressSearch
    @PageSize INT = 50,
    @Start INT = 1,
    @StreetName NVARCHAR(100) = '',
    @Esn INT = NULL,
    @Notes NVARCHAR(500) = '',
    @ParcelNumber NVARCHAR(50) = '',
    @ZoningCode NVARCHAR(50) = '',
    @HouseNumber NVARCHAR(25) = '',
    @County NVARCHAR(100) = '',
    @Municipality NVARCHAR(50) = '',
    @UniqueId INT = NULL,
    @District INT = NULL,
    @SubDivision NVARCHAR(100) = ''
AS
BEGIN
    ;WITH CTE AS
    (
        SELECT 
            Id
            , HouseNumber
            , StreetName
            , EsnNumber
            , PropertyDescription AS Description
            , TotalAddress
            , PointType
            , ROW_NUMBER() OVER (ORDER BY OBJECT_NAME(Id)) AS RowNumber
        FROM
            dbo.AddressJoinedToAllTables
        WHERE 
            (StreetName LIKE '%' + @StreetName + '%' OR @StreetName = '')
            AND (Notes LIKE '%' + @Notes + '%' OR @Notes = '')
            AND (ParcelNumber LIKE '%' + @ParcelNumber + '%' OR @ParcelNumber ='')
            AND (Zoning LIKE '%' + @ZoningCode + '%' OR @ZoningCode = '')
            AND (HouseNumber LIKE '%' + @HouseNumber + '%' OR @HouseNumber ='')
            AND (County LIKE '%' + @County + '%' OR @County = '')
            AND (Municipality LIKE '%' + @Municipality + '%' OR @Municipality ='')
            AND (SubDivision LIKE '%' + @SubDivision +'%' OR @SubDivision = '')
            AND (CAST(EsnNumber AS NVARCHAR) LIKE '%'+ CAST(@Esn AS nvarchar) + '%' OR @Esn IS NULL)
            AND (CAST(District AS NVARCHAR) LIKE '%'+ CAST(@District AS nvarchar) + '%' OR @Esn IS NULL)
            AND (Id = @UniqueId OR @UniqueId IS NULL)
    )
    SELECT 
        Id
        , HouseNumber
        , StreetName
        , EsnNumber
        , Description
        , TotalAddress
        , PointType
    FROM
        CTE
    WHERE 
        RowNumber BETWEEN @Start AND (@Start + @PageSize-1)
END

0 个答案:

没有答案