Never ever Concatenate strings to generate a query tattoo this thing on your body.
Here is how your code and Queries should interact with SQL server.
private int InsertRecord(string articleName, string articleContent, string cId, string img, string url)
{
int rA;
using (var con = new SqlConnection(@"YOUR CONNECTION STRING GOES HERE"))
{
using(var cmd = new SqlCommand("INSERT INTO Articles (article_name, article_content, category_id, img, url) values (@aName, @aContent, (SELECT category_id FROM categories WHERE categories.category_id = @cId), @img, @url)", con))
{
cmd.Parameters.AddWithValue("@aName", articleName);
cmd.Parameters.AddWithValue("@aContent", articleContent);
cmd.Parameters.AddWithValue("@cId", cId);
cmd.Parameters.AddWithValue("@img", img);
cmd.Parameters.AddWithValue("@url", url);
con.Open();
rA = cmd.ExecuteNonQuery();
con.Close();
}
}
return rA;
}
ADO.NET
is highly vulnerable to SQL Injections
if you go by the string concatenation for your query.
Another thing is to use Stored Procedures
for sql again that will also based on Parameterizing the SqlCommand
.