我想问一下其他程序员如何生成动态SQL字符串以作为SQLCommand对象的CommandText执行.
我正在生成包含用户生成的"WHERE"子句和SELECT字段的参数化查询.有时查询很复杂,我需要对如何构建不同部分进行大量控制.
目前,我使用许多循环和switch语句来生成必要的SQL代码片段并创建所需的SQL参数对象.这种方法很难遵循,它使维护成为一项真正的苦差事.
这样做有更清洁,更稳定的方法吗?
有什么建议??
编辑:要添加我以前的帖子的详细信息:
由于要求,我无法真正模拟我的查询.它只是变化太大了.
我将展示一些代码(恐怖!),以便你们知道我正在处理什么.
sqlCmd.CommandText = "DECLARE @t Table(ContactId int, ROWRANK int" + declare
+ ")INSERT INTO @t(ContactId, ROWRANK" + insertFields + ")"//Insert as few cols a possible
+ "Select ContactID, ROW_NUMBER() OVER (ORDER BY " + sortExpression + " "
+ sortDirection + ") as ROWRANK" // generates a rowrank for each row
+ outerFields
+ " FROM ( SELECT c.id AS ContactID"
+ coreFields
+ …Run Code Online (Sandbox Code Playgroud) 当数据键没有要发送的数据时,以下代码正确执行,即数据:"{}"空JSON对象,而webservice不带参数.我想将一些数据发布到网络服务,但我遇到了麻烦.
当我尝试将其设置为数据时:"{'name':'Niall','surname':'Smith'}",我收到错误
{"Message":"Invalid web service call, missing value for parameter: \u0027json\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
Run Code Online (Sandbox Code Playgroud)
Web服务未执行.
这是我的Jquery调用,将我的数据发布回服务器.
$.ajax({
type: "POST",
url: "/WebServices/BasketServices.asmx/AddItemToBasket",
data: "{'name':'niall'}", // Is this Correct??
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnItemAddedSuccess
});
function OnItemAddedSuccess(result,eventArgs) {
//deserialize the JSON and use it to update the Mini Basket
var response = JSON.parse(result.d);
}
Run Code Online (Sandbox Code Playgroud)
这是我的WebService
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] …Run Code Online (Sandbox Code Playgroud) 我听说人们使用它们来跟踪会话变量,但我真的想知道它们是否有很多用途,在什么条件下使用哈希表与任何其他可以处理键值对的数据结构都是有益的,比如字典.
例如.我听说过人们将会话值放在哈希表中,然后将哈希表放在Session对象中.我只是想知道那是什么好处.
- 性能更高吗? - 它是否可以防止其他开发人员将同名变量放入会话中?
编辑.