Gustavo

Gustavo

  • NA
  • 1.3k
  • 449.5k

How do I: Create a class to do some database functions?

Mar 18 2010 2:03 PM

Hello:
 
I am trying to clean my code and would like to put some of it in a class. Below is my code and what I would like to do. Hope its not too much to post.
 

Main program:

                Currently I have this to call the connection:

        private void buttonDBConnect_Click(object sender, EventArgs e)

        {

 

            DBServer = textBoxProfileServer.Text;

            DBDatabase = textBoxProfileDatabase.Text;

            DBLogin_ID = textBoxProfileLogin_ID.Text;

            DBPassword = textBoxProfilePassword.Text;

            //

            ClassDB.Connection(DBServer, DBDatabase, DBLogin_ID, DBPassword);

        }

 

                This fills my dataGrid:

        private void buttonLoadGrid_Click(object sender, EventArgs e)

        {

            DBCommand =

                " SELECT"

                + " [Name]"

                + " ,[Address_1]"

                + " FROM"

                + " [Company]"

                ;

            ClassDB.Select(DBCommand);

            //

            SqlDataAdapter DBDataAdapter = new SqlDataAdapter(ClassDB.DBSelect, ClassDB.DBConnect);

            DataSet DBDataSet = new DataSet();

            DBDataAdapter.Fill(DBDataSet);

            //

            dataGridViewGrid.DataSource = DBDataSet.Tables[0];

            dataGridViewGrid.Refresh();

        }

 

Class:

            This does the connection:

        public static void Connection(string DBServer, string DBDatabase, string DBLogin_ID, string DBPassword)

        {

            //MessageBox.Show("DBClass: Connection");

            DBConnect =

                @"Server=" + DBServer + ";"

                + "Database=" + DBDatabase + ";"

                + "User ID=" + DBLogin_ID + ";"

                + "Password=" + DBPassword + ";";

            //MessageBox.Show("DBConnect: With:" + DBConnect);

            //

            try

            {

                //MessageBox.Show("TRY...");

                SqlConnection DBConnection = new SqlConnection();

                DBConnection.ConnectionString = DBConnect;

                DBConnection.Open();

                DBConnection.GetSchema();

            }

            catch

            {

                //MessageBox.Show("CATCH...");

            }

            finally

            {

                //MessageBox.Show("FINALLY...");

            }

        }

 

//////////////////////////////////////////////////////////////////////////////////////////////////

 

What I would like to do is to call my DBClass.LoadGrid. By calling it like this: DBClass.LoadGrid(DBCommand);

This is what I have in my DBClass.LoadGrid now. What should I have in it?

 

        public static void LoadGrid(string DBCommand)

        {

            //MessageBox.Show("DBLoadGrid: With:" + DBCommand);

            //

            try

            {

                //MessageBox.Show("TRY...");

                DBLoadGrid = DBCommand;

            }

            catch

            {

                //MessageBox.Show("CATCH...");

            }

            finally

            {

                //MessageBox.Show("FINALLY...");

            }

        }

 

 
 


Answers (37)