2016-12-17 1 views
-4

私は なぜこのクエリは機能しませんか?

以下
<asp:Label ID="Label1" runat="server" Text="Email'e Göre Silin"></asp:Label> 

     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
     <br /> 
     <br /> 
     <asp:Button ID="Button1" runat="server" Text="Sil" /> 

とテキストボックスとボタンを追加しましたが、私のクエリは、これらのために働くではありません。

SqlConnection con = new SqlConnection("Data Source=DESKTOP-VQUBBVP\\SQLEXPRESS; initial catalog=UgurBocegiDatabase; Integrated Security=True"); 
      con.Open(); 
      SqlCommand cmd = new SqlCommand("delete from tblMessage where Email = '"+TextBox1.Text+ "' ", con); 
      cmd.ExecuteNonQuery(); 

以下のように追加しようとしましたが、もう一度動作しませんでした。

SqlCommand cmd = new SqlCommand("delete from tblMessage where Email = '"+TextBox1.Text.ToString()+ "' ", con); 

とクエリが

delete from tblMessage where Email = 'gs213' 

問題は何である以下のようにSQL Serverで動作しますか?

+4

**警告**あなたのコードは、SQLインジェクション攻撃に対して非常に脆弱です。 –

+0

決して、家庭でこれを行うことはありません。 – TigOldBitties

+2

[尋ねる]を読んで、「うまくいかない」ことを詳述してください。 – CodeCaster

答えて

0

これは機能します。

文字列連結の代わりに、以下のようなパラメータを使用できます。

using (SqlConnection connection = 
        new SqlConnection(ConfigurationManager.ConnectionStrings["DEFAULT"].ConnectionString)) 
      { 
       var command = new SqlCommand("delete from tblMessage where email = @email", connection); 
       command.Parameters.Add(new SqlParameter("email", SqlDbType.VarChar) 
       { 
        Value = TextBox1.Text 
       }); 
       connection.Open(); 
       command.ExecuteNonQuery(); 
      } 
+0

は@ – Khatibzadeh

関連する問題