动态存储过程无法在Web服务中运行

时间:2012-10-22 10:21:53

标签: c# asp.net sql-server web-services stored-procedures

我有一个动态选择存储过程。而且我在我的网络应用程序中完美地使用它。 但我需要一个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

1 个答案:

答案 0 :(得分: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)+ ''') ';