0

動的選択ストアドプロシージャがあります。そして、私はそれを私のWebアプリケーションで完全に使用しています。しかし、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;
}

これはdbの私のクラスです。

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 に答える