public static DataSet execute_query(string query)
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection();
con.ConnectionString = DataAccessLayer.Properties.Settings.Default.cn;
try
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(query, con);
da.Fill(ds);
}
catch (Exception ex)
{
ds = null;
}
finally
{
if (con.State == ConnectionState.Open) con.Close();
}
return ds;
}
|
|||||
closed as unclear what you're asking by forsvarir, Mast, Pimgd, mdfst13, Jamal♦ Sep 14 '16 at 18:55Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question. |
|||||
|
The best way to ensure
The Slightly more advanced is the ADO.NET If you're not constrained to .net 2.0 then the next best thing would be LINQ-to-SQL, or Entity Framework (or NHibernate, or Dapper, depending on your needs): these more modern tools often eliminate the need to ever concatenate SQL strings, or even to explicitly create
Generates a SQL query similar to:
...without ever needing to explicitly create a parameter or concatenate an arbitrary string into a bit of SQL - and C# looks so much better without SQL string literals all over the place! And the best part, it returns a But it's great that you're starting with the lower-level stuff: lots of people start with Entity Framework and don't quite understand what's happening under the hood. Other issues:
|
|||||||||||||||||||||
|
There are two problems with this that are immediately apparent:
|
|||||||||||||
|
As stated by others this is open to SQL injection attack To make it more generalized consider passing the connection string Not going to have correct syntax every time. Pass the SQL error back.
|
|||
|