1 Replies - 477 Views - Last Post: 30 May 2009 - 03:55 PM Rate Topic: -----

#1 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 61
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Question regarding SQL Basic in C#

Post icon  Posted 30 May 2009 - 11:12 AM

I have these three methods to connect to an SQL DB:

public static string GetConnectionString(string strConnection)
        {
            //variable to hold our connection string for returning it
            string strReturn = "";
            //check to see if the user provided a connection string name
            //this is for if your application has more than one connection string
            if (!string.IsNullOrEmpty(strConnection)) //a connection string name was provided
            {
                //get the connection string by the name provided
                strReturn = ConfigurationManager.ConnectionStrings[strConnection].ConnectionString;
            }
            else //no connection string name was provided
            {
                //get the default connection string
                strReturn = ConfigurationManager.ConnectionStrings["SQLBasic.Properties.Settings.MovieDBConnectionString"].ConnectionString;
            }
            //return the connection string to the calling method
            return strReturn;
        }

        /// <summary>
        /// Returns a BindingSource, which is used with, for example, a DataGridView control
        /// </summary>
        /// <param name="cmdSql">"pre-Loaded" command, ready to be executed</param>
        /// <returns>BindingSource</returns>
        /// <remarks>Use this function to ease populating controls that use a BindingSource</remarks>
        public static BindingSource GetBindingSource(SqlCommand SQLCommand)
        {
            BindingSource oBindingSource = new BindingSource();
            SqlDataAdapter daGet = new SqlDataAdapter(SQLCommand);
            DataTable dtGet = new DataTable();

            SQLCommand.CommandTimeout = 240;
            dtGet.Locale = System.Globalization.CultureInfo.InvariantCulture;

            try
            {
                daGet.Fill(dtGet);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error in GetBindingSource");
                return null;
            }

            oBindingSource.DataSource = dtGet;
            return oBindingSource;           
        }

        public static void HandleConnection(SqlConnection oCn)
        {
            //do a switch on the state of the connection
            switch (oCn.State)
            {
                case ConnectionState.Open: //the connection is open
                    //close then re-open
                    oCn.Close();
                    oCn.Open();
                    break;
                case ConnectionState.Closed: //connection is open
                    //open the connection
                    oCn.Open();
                    break;
                default:
                    oCn.Close();
                    oCn.Open();
                    break;
            }
        }


My question is on the GetBindingSource method. Does it work by just passing it the name of the stored procedure name and it'll return the binding source for me to attach to a DataGridView?

I'm reading the Tut by PsychoCoder but I see he creates a connection string in a lot of methods and creates a command in all the methods. Isn't it better to just create 1 connection string and one binding source so you pass on the Command to that method?

Thanks for the information.

Is This A Good Question/Topic? 0
  • +

Replies To: Question regarding SQL Basic in C#

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Question regarding SQL Basic in C#

Posted 30 May 2009 - 03:55 PM

That method, you pass a SqlCommand object (after you've set the CommandText, CommandType properties and added any parameters for the stored procedure) and it returns a BindingSource object
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1