Ich muss Text aus einem asp.net-Webformular einfügen. Die letzte Einfügung in meiner Zeichenfolge muss den Wert aus einer Spalte basierend auf einem überprüften TreeView-Wert einfügen. Ich muss eine Unterabfrage in meiner Einfügeanweisung ausführen, die SQL-Injektion vermeidet. Der aktuelle Code fügt einen Nullwert und nicht den Wert aus der SELECT-Anweisung ein.Einfügen von Abfrage mit Sub-Abfrage asp.net
Dies ist mein Code:
string content = TxtContent.Text;
string Pagename = txtKeywords.Text;
string title = TxtPageName.Text;
string description = TxtDescription.Text;
string URL = TxtPageName.Text;
string Keywords = txtKeywords.Text;
string tree = TreeView1.SelectedValue;
string query = @"INSERT INTO Menus([content], [Pagename], [title], [description], [Keywords], [url], [ParentMenuId]) " + "Values('" + content + "', '" + Pagename + "', '" + title + "', '" + description + "', '" + Keywords + "', '/" + URL + "', (Select [parentmenuid] from [menus] where [title] ='"+tree+"'))";
using (SqlConnection connection = new SqlConnection("Data Source=MYConnectionString"))
{
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
command.ExecuteNonQuery();
}
}
Bitte werfen Sie einen Blick auf [SQL-Injection-Angriffe] (https://en.wikipedia.org/wiki/SQL_injection), weil Ihre aktuellen Code mit diesem Problem ist gespickt. –