C#如何解析存储过程字符串
我有一个来自客户端应用程序的查询字符串。它带有所有参数,例如
string query="PROCS.DBO.APP_2370_ANALYST_S 'ABC' , 'TESTDATA' , 100";
在服务器中,我创建了一个函数(Util.getParametersFromString)来解析来自客户端应用程序的字符串,以使用 string.Split 函数创建参数对象数组。 我使用“,”和“”作为分隔符来创建对象数组。
我通过使用下面的代码执行数据库过程,
object[] parameters = Util.getParametersFromString(query);
DbCommand cmd = dbconnection.GetStoredProcCommand("PROCS.DBO.APP_2370_ANALYST_S", parameters);
如果参数字符串不包含逗号或单引号,我工作得很好。 如果参数字符串之一有一个或多个逗号或单引号。 就像下面的
string query="PROCS.DBO.APP_2370_ANALYST_S 'A,B,C' , 'Hi, Sam 'The Legend' Brown was here ' , 100";
参数数组没有正确出现一样。我不知道在这种
情况下如何正确解析字符串。请给我建议来解决这个问题,
我英语不好。所以我很抱歉如果我没有正确写下我的问题
问候, 公园
I have a Query string from client application. It comes with all parameters like
string query="PROCS.DBO.APP_2370_ANALYST_S 'ABC' , 'TESTDATA' , 100";
In Server, I made a function(Util.getParametersFromString) to parse string from client application to make parameter object Array using string.Split function.
I used ',' and ' ' as separator to make object array.
And I execute db procedure by using below code
object[] parameters = Util.getParametersFromString(query);
DbCommand cmd = dbconnection.GetStoredProcCommand("PROCS.DBO.APP_2370_ANALYST_S", parameters);
I works well if parameter string doesn't contain comma or single quotation mark.
If one of parameter string have one or more comma or single quotaion mark.
Like below
string query="PROCS.DBO.APP_2370_ANALYST_S 'A,B,C' , 'Hi, Sam 'The Legend' Brown was here ' , 100";
parameter array didn't come correctly. I didn't know how to parse string correctly in this
situation. Please give me advice to solve this problem
I am not good at english. So I am so sorry If I didn't write my question correctly
Regards,
Park
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以转义单引号 -
'
变为''
:至于逗号的问题 - 这取决于您的函数是如何编写的。您必须转义逗号并确保您的函数知道此转义序列。
You can escape the single quotes -
'
becomes''
:As for the problem with a comma - it depends on how your function is written. You will have to escape the comma and make sure your function is aware of this escape sequence.
如果查询字符串的两个参数都像示例一样灵活,并且您无法按照 Oded 的回答,你有问题。
例如,查询
"PROCS.DBO.APP_2370_ANALYST_S 'ABC' , 'ABC' , 'ABC' , 100"
可以解释为具有第一个参数"'ABC' , 'ABC '"
和第二个参数"ABC"
,反之亦然。另一方面,如果您的第一个参数可能不包含
'
,那么您可以通过查看前两个'
和第二个参数来识别第一个参数落在第三个和最后一个'
之间。If both parameters of your query string are as flexible as your example, and you cannot change the way this string is generated as suggested in Oded's answer, you have a problem.
The query
"PROCS.DBO.APP_2370_ANALYST_S 'ABC' , 'ABC' , 'ABC' , 100"
for example, could be interpreted as having the first parameter"'ABC' , 'ABC'"
and second parameter"ABC"
or vice versa.If, on the other hand, your first parameter may not contain
'
s, then you could identify the first parameter by looking between the first two'
s, and the second parameter by falling between the third and the last'
.