[c#] '가있는 곳에'를 추가하는 프로그램
Maybe I'm missing something but why not simply use the Replace method?
insertStatement = insertStatement.Replace("'","''");
-------------------What do you expect your code to do? Do you expect this:
VALUES ('test', 'test2')
to turn into this?
VALUES (''test'', ''test2'')
Surely that is invalid SQL; surely you don’t want that.
You actually want to escape the strings first, and then construct the SQL query:
var escapedString1 = escape("test");
var escapedString2 = escape("test2");
string insertStatement = string.Format(
"INSERT INTO EXCEPTION_LOG (value1, value2) VALUES ({0}, {1});",
escapedString1, escapedString2);
Now writing the escaping function is really easy:
public static string escape(string input)
{
return "'" + input.Replace("'", "''") + "'";
}
-------------------That code is a very very long way about doing it, you can just run a replace on all ' characters and replace with ''. Code below:
private static string AddEscapeStrings(string insertStatement)
{
var returnString = returnString.Replace("'","''");
return returnString;
}
-------------------이전 문자를 c [i-1] = '\' '로 덮어 씁니다.
대신 삽입해야합니다.
-------------------먼저 '@'synax를 사용하여 줄을 끊으십시오.
string insertStatement = @"INSERT INTO EXCEPTION_LOG (value1, value2)
VALUES ('test', 'test2');";
둘째, 왜 double로 요청을 무효화해야 '
합니까?
마지막으로 replace를 사용할 수 있습니다.
insertStatement = insertStatement.Replace("'","''");
''
이미 성명서에 없는 경우 .
다음을 사용해야합니다.
private static string AddEscapeStrings(string insertStatement)
{
return insertStatement.Replace("'", "''");
}
-------------------문자열에 직접 삽입 할 수 있습니다.
string result = "";
foreach(char c in insertStatement)
{
if(c == '\'')
result += "'";
result += c;
}
return result;
편집 : 나는 당신이 SQL 쿼리를 수행하는 부분을 놓쳤습니다-매개 변수를 사용해야합니다. 읽어야 할 링크는 다음과 같습니다. http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx
출처
https://stackoverflow.com/questions/7415060