0

我有一个动态选择存储过程。我在我的网络应用程序中完美地使用了它。但是我需要一个 Web 服务,当我对其进行编码和调试时,它给了我一个错误。

我正在发送完全相同的参数,但是当 Web 应用程序运行良好时,Web 服务给出了一个语法错误,例如incorrect syntax near and..

为什么会这样?

try
{
    SqlParameter[] sp = new SqlParameter[8];
    sp[0] = new SqlParameter("@tarih", DateTime.Now);
    sp[1] = new SqlParameter("@aractipkamyon", true);
    sp[2] = new SqlParameter("@aractipkamyonet", true);
    sp[3] = new SqlParameter("@aractiptir", true);
    sp[4] = new SqlParameter("@nereden", 0);
    sp[5] = new SqlParameter("@nereye", 0);
    sp[6] = new SqlParameter("@yuktipadi", null);
    sp[7] = new SqlParameter("@odemetipadi", "Pesin-Teslimde");
    DataTable dtProc = dbHelper.runQueryWithParams("prIlanHizliArama2", sp);
    if (dtProc.Rows.Count > 0)
    { }
}
catch (Exception)
{
    throw;
}

这是我的分贝课程。

public static DataTable runQueryWithParams(string storedProcedureName, params SqlParameter[] arrParam)
{
    DataTable dt = new DataTable();
    //SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["localhost"].ConnectionString);
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
    SqlCommand cmd = new SqlCommand(storedProcedureName, conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Clear();
    if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
        conn.Open();

    try
    {
        if (arrParam != null)
        {
            foreach (SqlParameter param in arrParam)
                cmd.Parameters.Add(param);
        }

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        SqlDataReader dr = cmd.ExecuteReader();
        dt.Load(dr);
        return dt;
    }
    catch (Exception ex)
    {
        throw new Exception("Error: " + ex.Message);
    }
    finally
    {
        cmd.Dispose();
        conn.Close();
    }
}

SQL:

ALTER proc [dbo].[prIlanHizliArama2]
@tarih nvarchar(MAX) = NULL,
@aractipkamyon bit= NULL,
@aractipkamyonet bit= NULL,
@aractiptir bit= NULL,
@nereden int = NULL,
@nereye int = NULL,
@yuktipadi nvarchar(10) = NULL,
@odemetipadi nvarchar(25) = NULL
as
begin
declare @sqlQuery as nvarchar(4000);

select @sqlQuery = 'select tbAracTip.tipadi as tip1, tip2.tipadi as tip2,
tbIlan.ilanId, tbYukTipi.tipadi as yuktipi, tbIlan.plaka, tbIlan.nereden, tbIlan.nereye,
tbIlan.fiyat, tbIlan.tarih, tbOdemeTipi.tipadi as odemetipi, tbIlce.lats, tbIlce.lngs
from tbIlan left join tbYukTipi on tbIlan.yuktipiId = tbYukTipi.yuktipiId
left join tbOdemeTipi on tbIlan.odemetipiId = tbOdemeTipi.odemeId
left join tbArac on tbArac.plaka = tbIlan.plaka
left join tbAracTip on tbArac.aractipId = tbAracTip.aractipId
left join tbAracTip as tip2 on tip2.aractipId = tbArac.usttipId
left join tbIlce on tbIlan.nereden = tbIlce.ilceId WHERE 1=1';

if (@nereden is not null and @nereden <> 0)
select @sqlQuery = @sqlQuery + ' and (tbIlan.nereden = ' + CONVERT(nvarchar(5),@nereden+') ');
if (@nereye is not null and @nereye <> 0)
select @sqlQuery = @sqlQuery + ' and (tbIlan.nereye = ' + convert(nvarchar(5), @nereye) + ') ';
if (@yuktipadi is not null)
select @sqlQuery = @sqlQuery + ' and (tbYuktipi.tipadi = ' + convert(nvarchar(500),@yuktipadi)+ ') ';
if (@odemetipadi is not null)
select @sqlQuery = @sqlQuery + ' and (tbOdemetipi.tipadi like ' +'''%'+ convert(nvarchar(500),@odemetipadi)+ '%''' + ')';
if (@tarih is not null)
select @sqlQuery = @sqlQuery + ' and (DATEDIFF(DD,tbIlan.tarih,convert(date, '''+@tarih+''' ,103)) = 0) ';
if (@aractipkamyon = 0 and @aractipkamyonet = 0 and @aractiptir = 1)
select @sqlQuery = @sqlQuery + ' and (tbArac.aracId in (select arac1Id from tbTir)) ';

if (@aractipkamyon = 0 and @aractipkamyonet = 1 and @aractiptir = 0)
select @sqlQuery = @sqlQuery + ' and (tip2.tipadi = ' + '''Kamyonet'') ';

if (@aractipkamyon =0 and @aractipkamyonet =1 and @aractiptir = 1)
select @sqlQuery = @sqlQuery + ' and ((tip2.tipadi = ' + '''Kamyonet'')' + ' or (tbArac.aracId in (select arac1Id from tbTir))) ';

if (@aractipkamyon =1 and @aractipkamyonet =0 and @aractiptir = 0)
select @sqlQuery = @sqlQuery + ' and (tip2.tipadi = ' + '''Kamyon'') ' ;

if (@aractipkamyon =1 and @aractipkamyonet = 0 and @aractiptir = 1)
select @sqlQuery = @sqlQuery + ' and ((tip2.tipadi = ' + '''Kamyon'')' + ' or (tbArac.aracId in (select arac1Id from tbTir))) ';

if (@aractipkamyon =1 and @aractipkamyonet =1 and @aractiptir = 0)
select @sqlQuery = @sqlQuery + ' and ((tip2.tipadi = ' + '''Kamyon'')' + ' or (tip2.tipadi = ' + '''Kamyonet'')) ' ;

if (@aractipkamyon =1 and @aractipkamyonet =1 and @aractiptir = 1)
select @sqlQuery = @sqlQuery + ' and ((tip2.tipadi = ' + '''Kamyon'')' + ' or (tip2.tipadi = ' + '''Kamyonet'')' + ' or (tbArac.aracId in (select arac1Id from tbTir))) ';

print (@sqlQuery)
exec (@sqlQuery)
end
4

1 回答 1

1

找到了。如果我发送空字符串,它会给我一个错误,因为这一行:

if (@yuktipadi is not null)
select @sqlQuery = @sqlQuery + ' and (tbYuktipi.tipadi = ' + convert(nvarchar(500), @yuktipadi)+ ') ';

我这样替换它:

if (@yuktipadi is not null)
    select @sqlQuery = @sqlQuery + ' and (tbYuktipi.tipadi = ''' + convert(nvarchar(500), @yuktipadi)+ ''') ';
于 2012-10-22T10:54:28.363 回答