Solarion
Honorary Master
- Joined
- Nov 14, 2012
- Messages
- 28,051
- Reaction score
- 17,805
I'm fairly newish to C#(About 3 months) and I'm trying to put together a list of functions for every day use. Please post the ones you guys/girls find helpful and use often, either pre-defined or ones you've created. I'll start
I use this function to populate a DataTable.
USE
I use this function to populate a DataTable.
Code:
public DataTable GetRecords(String sql)
{
String myConnString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType = CommandType.Text;
myCommand.Connection = myConnection;
myCommand.CommandText = sql;
try
{
myConnection.Open();
DataTable tbl = new DataTable();
SqlDataAdapter sqlDataReader = new SqlDataAdapter();
sqlDataReader.SelectCommand = myCommand;
sqlDataReader.Fill(tbl);
return tbl;
}
catch (Exception ex)
{
throw ex;
}
finally
{
myConnection.Close();
}
}
USE
Code:
string sql = "SELECT * From tblPersons WHERE User_ID = '12345'";
DataTable tbl = new DataTable();
tbl = GetRecords(sql);