2011-11-13 13 views
-1

私はmysqlデータベースにパス名を挿入しようとしましたが、その動作は変です。私が見つけたのは、私のプロジェクトディレクトリのパスが何か変わったということです。私のプログラムを使用すると、結果は "D:musicFOREIGN !!新しいfolderarat cakepHilary Duff - Wake Up.mp3"となります。 phpmyadminから手動でクエリを挿入すると、その結果 "D:\ music \ FOREIGN !! \ New folder \ barat cakep \ Hilary Duff - Wake Up.mp3"、何も変わりません。私は間違ったことをしましたか?ここに私のコードは次のとおりです。MySqlCommand.ExecuteNonQueryエラーと奇妙なC#

public static void add_song(song input) 
    { 
     establised(); 
     try 
     { 
      MySqlCommand command = connection.CreateCommand(); 
      command.CommandText = string.Format("INSERT INTO song (ID_SONG, ID_GENRE, ID_CATEGORY, SONG_TITLE, SONG_ARTIST, SONG_LOCATION, SONG_PLAYED) select '', b.id_genre, c.id_category, '{0}', '{1}', '{2}', '0' from genre b, category c where b.genre = '{3}' and c.category = '{4}' ", input.title, input.artist, input.location, input.genre, input.category); 
      command.ExecuteNonQuery(); 
     } 
     catch (Exception e) { } 
     release(); 
    } 

、ここでは、私の例のクエリです:

"INSERT INTO song (ID_SONG, ID_GENRE, ID_CATEGORY, SONG_TITLE, SONG_ARTIST, SONG_LOCATION, SONG_PLAYED) select '', b.id_genre, c.id_category, 'Wake Up', 'Hilary Duff', 'D:\\music\\FOREIGN!!\\New folder\\barat cakep\\Hilary Duff - Wake Up.mp3', '0' from genre b, category c where b.genre = 'Pop' and c.category = 'international'" 

答えて

3

ので、自動的にチェックされている、クエリにパラメータを渡すためにMysqlParameterオブジェクトを使用します。

// 1. Use parameters label in the query 
command.CommandText = "INSERT INTO song (ID_SONG, ID_GENRE, ID_CATEGORY, SONG_TITLE, SONG_ARTIST, SONG_LOCATION, SONG_PLAYED) select '', b.id_genre, c.id_category, @Title, @Artist, @Location, '0' from genre b, category c where b.genre = @GenRe and c.category = @category " 

// 2. Define parameters used in command object 
    MySqlParameter param = new MySqlParameter(); 
    param.ParameterName = "@Location"; 
    param.Value   = input.location; 

//3. Assign the parameter to the command 
command.Parameters.Add(param); 

//GO ahead with others parameters ... 
+0

桶である変数に関数を適用します。私はそれを試してみます..^_^ –

+0

その作品.. !!!^_^ ありがとうございます.. –

+1

答えがあなたの問題を解決する場合は、それを受け入れることを検討することができます;) – aleroot

0

クエリ文字列にあるテキスト変数をエスケープするだけで済みます。

エスケープする必要がある2文字は、アポストロフィ( ')とスラッシュ(\)です。

Public Shared Function FixSQL(item As String) As String 
    Dim result As String 

    result = item 
    result = Replace(result, "\", "\\") 
    result = Replace(result, "'", "''") 
    return result 
End Function 

シンプルな「FixSQL」関数を作成し、あなたのSQLクエリに