Is it possible to write sql sentences in the back-end?
You know when making a dynamic website, there will allways be tons of SQL sentences.
i was wondering if it was possible to just have 2-3 SQL querys in a class file, and write the rest in the back-end?
Maybe like this.
public DataTable SelectAllFrom(string query)
{
string query = @"SELECT * FROM ?query";
using (var cmd = new MySqlCommand(query))
{
cmd.Parameters.Add("?query", MySqlDbType.VarChar).Value = query;
return objConn.getData(cmd);
}
}
AND THEN in the code-behind:
string query = "tblAA WHERE fldAA=1";
var list = _objMethods.SelectAllFrom(query).Rows;
You can do it, but it’s not great practice. The more spread out your queries are, and the more SQL you mix in with your presentation and business-logic code, the more difficult it is to maintain. Your question makes it sound like you’re working with some type of layered/tiered architecture, and those exist for some very good reasons. I would try to avoid peppering your code with SQL without any organization; it might be quick and easy in the short run, but could lead to problems later.