2016-12-17 53 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(); 

我试着添加下面的.tostring方法,但它没有再次工作。

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

和查询SQL Server中的作用类似于下面

delete from tblMessage where Email = 'gs213' 

是什么问题?

+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