Webservice和sql

时间:2018-06-01 10:59:52

标签: sql sql-server wcf

我正在制作一个能够执行某些sql命令的Web服务,并以JSON格式输出结果,它有点起作用。但是现在我已经做了一些改变,而且我遇到了一个我似乎无法修复的问题。

我想要运行的SQL命令如下所示:

declare @param int
--set @param = 5

select u.SagsNr, uf.TekstWeb 
from UdlejningsSagFacilitet uf 
inner join UdlejningsSag u on uf.UdlejningsSagId = u.UdlejningsSagId 
where u.VisesPaaNettet = 1 and 
exists(select * from UdlejningsSagFacilitet uf inner join UdlejningsSag u on uf.UdlejningsSagId = u.UdlejningsSagId where TekstWeb like '%Oven%' and @param IS NULL OR u.SagsNr = @param) 
and @param IS NULL OR u.SagsNr = @param

基本上,当没有给出ID时我想要归还所有东西。这在SSMS中非常有效。然而!当我试图将它放入我的网络服务时,它不会起作用。我认为它应该是这样的:

SqlConnection con = new SqlConnection();
con = Database();

SqlDataAdapter daf = new SqlDataAdapter("select u.SagsNr, uf.TekstWeb " +
                                                    "from UdlejningsSagFacilitet uf " +
                                                    "inner join UdlejningsSag u on uf.UdlejningsSagId = u.UdlejningsSagId " +
                                                    "where u.VisesPaaNettet = 1 and exists(select * from UdlejningsSagFacilitet uf " +
                                                    "inner join UdlejningsSag u on uf.UdlejningsSagId = u.UdlejningsSagId " + 
                                                    "where TekstWeb like '%Oven%' and " + vSagsNr + " IS NULL OR u.SagsNr = " + vSagsNr + 
                                                    ") and " + vSagsNr + " IS NULL OR u.SagsNr = " + vSagsNr, con);

DataTable dtf = new DataTable();
daf.Fill(dtf);
string jsonf = GetJson(dtf);
return jsonf;

这里vSagsNr代替@param。

如果我为vSagsNr提供了一个值,但是如果我没有收到错误:

  Incorrect syntax near the keyword 'IS'.System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'IS'.
  ved System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
  ved System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
  ved System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
  ved System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
  ved System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
  ved System.Data.SqlClient.SqlDataReader.get_MetaData()
  ved System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption)
  ved System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
  ved System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
  ved System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
  ved System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
  ved System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
  ved System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
  ved System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
  ved System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
  ved System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
  ved WCFTest3.Service1.POSTBoligSøgning(Nullable`1 vSagsNr, String vFacilitet) i C:\Users\chm\source\repos\WCFTest3\WCFTest3\Service1.svc.cs:linje 54
ClientConnectionId:5bc85eb2-3e2a-46ef-b941-4419eab09b43
Error Number:156,State:1,Class:15

第54行是" daf.Fill(dtf);"部分。 我认为SQL在某种程度上是错误的,但我似乎无法弄明白。我做错了什么?

1 个答案:

答案 0 :(得分:1)

如果您没有为 var check = $('#checkBox:checked').length; 提供值,则查询"where TekstWeb like '%Oven%' and " + vSagsNr + " IS NULL的这一部分将失败。它看起来像这样:vSagsNr

如您所见,该查询不正确。因此,您最好的选择是将查询分成多个字符串,每次您要设置"where TekstWeb like '%Oven%' and IS NULL条件时检查其值,如果它为null或为空,则从查询字符串中完全删除该条件。 / p>

vSagsNr