Tim Maxey .NET Technology Blog & Resources

Dataset Function C#

// ########### START CODE ######################################
        public DataSet CreateDataSet(string strSQL)
        {

            System.Data.SqlClient.SqlConnection scnnNW = new System.Data.SqlClient.SqlConnection(GetConnectionString("connYourConnection"));
            // A SqlCommand object is used to execute the SQL commands.
            System.Data.SqlClient.SqlCommand scmd = new System.Data.SqlClient.SqlCommand(strSQL, scnnNW);
            // A SqlDataAdapter uses the SqlCommand object to fill a DataSet.
            System.Data.SqlClient.SqlDataAdapter sda = new System.Data.SqlClient.SqlDataAdapter(scmd);
            // Create and Fill a new DataSet.
            DataSet ds = new DataSet();
            sda.Fill(ds);
            scnnNW.Close();
            scnnNW.Dispose();
            return ds;

        }
// ########### END CODE ######################################

This function is used to get the connection form the .config file...

// ########### START CODE ######################################
static string GetConnectionString(string name)
        {
            // Assume failure.
            string returnValue = null;

            // Look for the name in the connectionStrings section.
            ConnectionStringSettings settings =
                ConfigurationManager.ConnectionStrings[name];

            // If found, return the connection string.
            if (settings != null)
                returnValue = settings.ConnectionString;

            return returnValue;
        }
// ########### END CODE ######################################


Feedback

No comments posted yet.


Post a comment





 

Please add 1 and 5 and type the answer here: