Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Consuming sqlcompact connection with using statement

Consuming sqlcompact connection with using statement

Scheduled Pinned Locked Moved C#
databasequestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    teknolog123
    wrote on last edited by
    #1

    hi, I use below code to create/consume and dispose sql connections.But as there are more than twenty methods/functions that I use it, same code block repeats. So, is it goog practice? Your valuable comments are welcomed.

    private void islemKayitlariniYukle(string select)
    {
    using (SqlCeConnection sqlCEBaglantisi = new SqlCeConnection(Properties.Settings.Default.sqlBag))
    using (SqlCeDataAdapter sqlAdaptor = new SqlCeDataAdapter(select, sqlCEBaglantisi))
    using (DataTable dtTablo = new DataTable())
    {
    sqlCEBaglantisi.Open();
    sqlAdaptor.Fill(dtTablo);

                        SqlCeCommand islemTipleriniAl = new SqlCeCommand("Select IslemTipi From IslemKaydi Group By IslemTipi", sqlCEBaglantisi);
                        using (SqlCeDataReader oku1 = islemTipleriniAl.ExecuteReader())
                        {
                            while (oku1.Read())
                            { cmbIGIslemTipiSec.Items.Add(oku1.GetString(0).Trim()); }
                        }
    
    L 1 Reply Last reply
    0
    • T teknolog123

      hi, I use below code to create/consume and dispose sql connections.But as there are more than twenty methods/functions that I use it, same code block repeats. So, is it goog practice? Your valuable comments are welcomed.

      private void islemKayitlariniYukle(string select)
      {
      using (SqlCeConnection sqlCEBaglantisi = new SqlCeConnection(Properties.Settings.Default.sqlBag))
      using (SqlCeDataAdapter sqlAdaptor = new SqlCeDataAdapter(select, sqlCEBaglantisi))
      using (DataTable dtTablo = new DataTable())
      {
      sqlCEBaglantisi.Open();
      sqlAdaptor.Fill(dtTablo);

                          SqlCeCommand islemTipleriniAl = new SqlCeCommand("Select IslemTipi From IslemKaydi Group By IslemTipi", sqlCEBaglantisi);
                          using (SqlCeDataReader oku1 = islemTipleriniAl.ExecuteReader())
                          {
                              while (oku1.Read())
                              { cmbIGIslemTipiSec.Items.Add(oku1.GetString(0).Trim()); }
                          }
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      teknolog123 wrote:

      same code block repeats.

      You could wrap some things in their own method :)

      teknolog123 wrote:

      So, is it goog practice?

      Not if you're religious about the DRY-principle. In that case, there'd be a factory to return an IDbConnection, and the query-string would be passed to a method containing the database-connection logic. Still, with or without the repeating code; I could work with it. If you'd use English names for your variables, I might even be able to understand it's intention :)

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      T 1 Reply Last reply
      0
      • L Lost User

        teknolog123 wrote:

        same code block repeats.

        You could wrap some things in their own method :)

        teknolog123 wrote:

        So, is it goog practice?

        Not if you're religious about the DRY-principle. In that case, there'd be a factory to return an IDbConnection, and the query-string would be passed to a method containing the database-connection logic. Still, with or without the repeating code; I could work with it. If you'd use English names for your variables, I might even be able to understand it's intention :)

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        T Offline
        T Offline
        teknolog123
        wrote on last edited by
        #3

        thanks let me explain more. I just want to dispose sqceConnection, sqlceCommand, SqlCeCommandBuilder and sqlceAdapter as soon as I finish processing the data. for example does the below code dispose the objects in that manner? I think no but wanna make sure. (note: I didn't use sqladapter and commnadbuilder below to keep the code short on purpose)

        public SqlCeConnection sqlSetCon()
        {
        SqlCeConnection sqlCon = new SqlCeConnection();
        sqlCon.ConnectionString = Properties.Settings.Default.sqlConnStr;
        sqlCon.Open();
        return sqlCon;
        }
        public DataTable returnDataTable(string selectString)
        {
        SqlCeDataAdapter sqlAdaptor = new SqlCeDataAdapter(selectString, sqlSetCon());
        SqlCeCommandBuilder sqlKomut = new SqlCeCommandBuilder(sqlAdaptor);
        DataTable dtTablo = new DataTable();
        sqlAdaptor.Fill(dtTablo);
        return dtTablo;
        }

            private void processList()
            {
               //does this using block dispose everything?
                using (DataTable dt = returnDataTable("Select \* From Customers Order By Surname"))
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        this.lblSurname.Text = dt.Rows\[i\]\["surname"\].ToString();
                    }
                }
            }
        
        L 1 Reply Last reply
        0
        • T teknolog123

          thanks let me explain more. I just want to dispose sqceConnection, sqlceCommand, SqlCeCommandBuilder and sqlceAdapter as soon as I finish processing the data. for example does the below code dispose the objects in that manner? I think no but wanna make sure. (note: I didn't use sqladapter and commnadbuilder below to keep the code short on purpose)

          public SqlCeConnection sqlSetCon()
          {
          SqlCeConnection sqlCon = new SqlCeConnection();
          sqlCon.ConnectionString = Properties.Settings.Default.sqlConnStr;
          sqlCon.Open();
          return sqlCon;
          }
          public DataTable returnDataTable(string selectString)
          {
          SqlCeDataAdapter sqlAdaptor = new SqlCeDataAdapter(selectString, sqlSetCon());
          SqlCeCommandBuilder sqlKomut = new SqlCeCommandBuilder(sqlAdaptor);
          DataTable dtTablo = new DataTable();
          sqlAdaptor.Fill(dtTablo);
          return dtTablo;
          }

              private void processList()
              {
                 //does this using block dispose everything?
                  using (DataTable dt = returnDataTable("Select \* From Customers Order By Surname"))
                  {
                      for (int i = 0; i < dt.Rows.Count; i++)
                      {
                          this.lblSurname.Text = dt.Rows\[i\]\["surname"\].ToString();
                      }
                  }
              }
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Something similar to below; wrap the disposable's in a using-section, and they'll be disposed when they go out of scope.

          public DataTable returnDataTable(string selectString)
          {
          using (var con = sqlSetCon())
          using (SqlCeDataAdapter sqlAdaptor = new SqlCeDataAdapter(selectString, con))
          {
          DataTable dtTablo = new DataTable();
          sqlAdaptor.Fill(dtTablo);
          return dtTablo;
          }
          }

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          T 1 Reply Last reply
          0
          • L Lost User

            Something similar to below; wrap the disposable's in a using-section, and they'll be disposed when they go out of scope.

            public DataTable returnDataTable(string selectString)
            {
            using (var con = sqlSetCon())
            using (SqlCeDataAdapter sqlAdaptor = new SqlCeDataAdapter(selectString, con))
            {
            DataTable dtTablo = new DataTable();
            sqlAdaptor.Fill(dtTablo);
            return dtTablo;
            }
            }

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            T Offline
            T Offline
            teknolog123
            wrote on last edited by
            #5

            thanks Eddy, by the way, is it only those that are written in parentheses disposed or anything in curly braces?

            L 1 Reply Last reply
            0
            • T teknolog123

              thanks Eddy, by the way, is it only those that are written in parentheses disposed or anything in curly braces?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              teknolog123 wrote:

              thanks Eddy, by the way, is it only those that are written in parentheses disposed or anything in curly braces?

              Anything that's disposable would be best in a using-clause. You can try it by putting it in a using, if it's not disposable you'll get a compiler-error saying so. Alternatively, you can check MSDN. If you can't limit it to a scope, then you'd best dispose it from code later.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups