Below is the most simple database query for C# to populate a GridView object:
int nItemID = 1; try { using (SqlConnection oSQLConn = new SqlConnection(ConfigurationManager.ConnectionStrings["UserInfo"].ConnectionString)) { oSQLConn.Open(); using (SqlCommand oSQLCmd = new SqlCommand()) { oSQLCmd.Connection = oSQLConn; oSQLCmd.CommandText = "SELECT DisplayName, EMail FROM Users WHERE UserID = @PARAM_ItemID"; oSQLCmd.Parameters.AddWithValue("@PARAM_ItemID", nItemID); SqlDataReader oSQLReader = oSQLCmd.ExecuteReader(); GridView1.DataSource = oSQLReader; GridView1.DataBind(); } } } catch (Exception ex) { Label_ErrorMsg.Text = ex.Message; }
There are actually many reasons to never use this, including performance, caching, safe SQL, code reuse, standards, proper exception handling, etc, etc. But sometimes you need a quick and dirty, non-production, piece of code for testing and/or development. This is it.
Remember Me
b, blockquote@cite, i, strike, strong, u
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.