6

「母の愛」のような(')引用符で構成される文字列があります...

C#からのSQLクエリによってデータを挿入している間。エラーが表示されます。どうすれば問題を修正してこの種のデータを正常に挿入できますか?

string str2 = "Insert into tblDesEmpOthDetails (EmpID, Interviewnotes) values ('" + EmpId + "','" + Interviewnotes + "')";

インタビューノートは、「母の愛」(一重引用符付き)のような値で構成されています。このクエリを実行しているときに、「文字列の後に閉じられていない引用符')」というエラーが表示されます。このタイプの文字列を挿入するにはどうすればよいですか?

4

6 に答える 6

22

SQLパラメータを使用しないと確信しています。

using (SqlCommand myCommand = new SqlCommand(
    "INSERT INTO table (text1, text2) VALUES (@text1, @text2)")) {

    myCommand.Parameters.AddWithValue("@text1", "mother's love");
    myCommand.Parameters.AddWithValue("@text2", "father's love");
    //...

    myConnection.Open();
    myCommand.ExecuteNonQuery();
    //...
}
于 2012-06-20T13:03:42.433 に答える
14

名前付きパラメーターとSqlParameterを使用します。

http://www.dotnetperls.com/sqlparameterから

class Program
{
    static void Main()
    {
        string dogName = "Fido";  // The name we are trying to match.

        // Use preset string for connection and open it.
        string connectionString = 
            ConsoleApplication1.Properties.Settings.Default.ConnectionString;

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            // Description of SQL command:
            // 1. It selects all cells from rows matching the name.
            // 2. It uses LIKE operator because Name is a Text field.
            // 3. @Name must be added as a new SqlParameter.
            using (SqlCommand command = 
               new SqlCommand("SELECT * FROM Dogs1 WHERE Name LIKE @Name", connection))
            {
                // Add new SqlParameter to the command.
                command.Parameters.Add(new SqlParameter("Name", dogName));

                // Read in the SELECT results.
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    int weight = reader.GetInt32(0);
                    string name = reader.GetString(1);
                    string breed = reader.GetString(2);
                    Console.WriteLine("Weight = {0}, Name = {1}, Breed = {2}", weight, name, breed);
                }
            }
        }
    }
}
于 2012-06-20T13:02:21.183 に答える
6

文字列内のすべての'文字を2つの'文字('')に置き換えることはできますが、それは良い考えではありません。この問題と他の多くの理由(SQLインジェクション攻撃の回避など)のため、文字列に直接連結して値を挿入ステートメントに追加するのではなく、名前付きパラメーターを使用する必要があります。例えば:

command.CommandText = "Insert into tblDesEmpOthDetails (EmpID, Interviewnotes) values (@EmpId, @Interviewnotes)";
command.Parameters.AddWithValue("EmpId", EmpId);
command.Parameters.AddWithValue("Interviewnotes", Interviewnotes);
于 2012-06-20T13:03:01.287 に答える
2

入力しようとしている文字列にこの行を追加します

文字列があったと言う

string test = "that's not working correctly"
test = replace(Request.QueryString(test), "'", "''")

その後、テストは今です

"that''s not working correctly"

これはSQLに対して構文的に正しいです

よろしく

于 2012-06-20T13:08:26.890 に答える
2

(非常に正確に)パラメータを示す回答の変形として:これが大変な作業のように思われる場合は、dapperなどのツールを使用して回避してください:

int empId = 123;
string notes = "abc";
connection.Execute(@"insert into tblDesEmpOthDetails (EmpID, Interviewnotes)
                     values (@empId, @notes)", new {empId, notes});

Dapperは自動的にempIdnotes匿名オブジェクトから)andを取得し、それらを名前付き/型付きパラメーターとして追加します。同様のQuery/ extension-methodsを使用すると、オブジェクトモデルに直接Query<T>簡単かつ高度に最適化されたクエリを実行することもできます。

于 2012-06-20T13:24:37.040 に答える
1

ダブル''を使用する必要があります

INSERT INTO something (Name) VALUES ('O''something''s')

これにより、O'something'sが挿入されます。私が読んだ別の例は次のとおりです。

文字列があるとしましょう:

SQL = "SELECT * FROM name WHERE LastName='" & LastName & "' "

O'Brian、O'Reilyなどの姓がある場合は、次のような文字列を取得します。

SELECT * FROM name WHERE LastName='O'Brien'

2番目の'はSQLステートメントを終了します。したがって、ここでの最も簡単な解決策は、double''を使用することです。そうすると、次のような文字列が作成されます。

SELECT * FROM name WHERE LastName='O''Brien'
于 2012-06-20T13:04:39.810 に答える