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. Database & SysAdmin
  3. Database
  4. Any way to optomize this?

Any way to optomize this?

Scheduled Pinned Locked Moved Database
question
4 Posts 3 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    public DataSet GetEntries() { //top 5 string ConnectionString = ConfigurationSettings.AppSettings.Get("connString"); SqlConnection SQLConnection = new SqlConnection(ConnectionString); string SQLView = "CREATE VIEW Content_View AS " + "SELECT TOP 10000 [HJ_BLog].* " + "FROM [HJ_BLog] " + "ORDER BY Date DESC "; SqlCommand SQLCommandCreateView = new SqlCommand(SQLView, SQLConnection); SqlDataAdapter DataAdapter = new SqlDataAdapter(SQLCommandCreateView); DataSet UserSet = new DataSet(); DataAdapter.Fill(UserSet); SQLCommandCreateView.Connection.Close(); string SQLQueryTop2 = "SELECT TOP 5 [Content_View].* " + "FROM [Content_View] "; SqlCommand SQLCommandTop2 = new SqlCommand(SQLQueryTop2,SQLConnection); SqlDataAdapter DataAdapterTop2 = new SqlDataAdapter(SQLCommandTop2); DataSet UserSetTop2 = new DataSet(); DataAdapterTop2.Fill(UserSetTop2); SQLCommandTop2.Connection.Close(); string SQLDropView = "DROP VIEW Content_View "; SqlCommand SqlDropView = new SqlCommand(SQLDropView,SQLConnection); SqlDropView.Connection.Open(); SqlDropView.ExecuteNonQuery(); SqlDropView.Connection.Close(); return UserSetTop2; } Pretty much using a view to sort then getting the top 5 from the view then deleting the view. Is there an easier way to do this? I am not sure the preformence hits on creating and droping views. or just a all around easier way to do this. thanks any points will be great

    M B 2 Replies Last reply
    0
    • L Lost User

      public DataSet GetEntries() { //top 5 string ConnectionString = ConfigurationSettings.AppSettings.Get("connString"); SqlConnection SQLConnection = new SqlConnection(ConnectionString); string SQLView = "CREATE VIEW Content_View AS " + "SELECT TOP 10000 [HJ_BLog].* " + "FROM [HJ_BLog] " + "ORDER BY Date DESC "; SqlCommand SQLCommandCreateView = new SqlCommand(SQLView, SQLConnection); SqlDataAdapter DataAdapter = new SqlDataAdapter(SQLCommandCreateView); DataSet UserSet = new DataSet(); DataAdapter.Fill(UserSet); SQLCommandCreateView.Connection.Close(); string SQLQueryTop2 = "SELECT TOP 5 [Content_View].* " + "FROM [Content_View] "; SqlCommand SQLCommandTop2 = new SqlCommand(SQLQueryTop2,SQLConnection); SqlDataAdapter DataAdapterTop2 = new SqlDataAdapter(SQLCommandTop2); DataSet UserSetTop2 = new DataSet(); DataAdapterTop2.Fill(UserSetTop2); SQLCommandTop2.Connection.Close(); string SQLDropView = "DROP VIEW Content_View "; SqlCommand SqlDropView = new SqlCommand(SQLDropView,SQLConnection); SqlDropView.Connection.Open(); SqlDropView.ExecuteNonQuery(); SqlDropView.Connection.Close(); return UserSetTop2; } Pretty much using a view to sort then getting the top 5 from the view then deleting the view. Is there an easier way to do this? I am not sure the preformence hits on creating and droping views. or just a all around easier way to do this. thanks any points will be great

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      public DataSet GetEntries()
      {
      string sql = "SELECT TOP 5 * FROM [HJ_BLog] ORDER BY Date DESC";
      string ConnectionString = ConfigurationSettings.AppSettings.Get("connString");
      SqlConnection conn;
      DataSet ds;
      SqlCommand cmd;
      SqlDataAdapter da;

      try
      {
      conn = new SqlConnection(ConnectionString);
      cmd = new SqlCommand(sql, conn);
      da = new SqlDataAdapter(cmd);
      ds = new DataSet();
      da.Fill(ds);
      }
      catch (Exception ex)
      {
      // do something...
      }
      finally
      {
      if (da != null) da.Dispose();
      if (cmd != null) cmd.Dispose();
      if (conn != null) conn.Dispose();
      }

      return ds;
      }

      L 1 Reply Last reply
      0
      • L Lost User

        public DataSet GetEntries() { //top 5 string ConnectionString = ConfigurationSettings.AppSettings.Get("connString"); SqlConnection SQLConnection = new SqlConnection(ConnectionString); string SQLView = "CREATE VIEW Content_View AS " + "SELECT TOP 10000 [HJ_BLog].* " + "FROM [HJ_BLog] " + "ORDER BY Date DESC "; SqlCommand SQLCommandCreateView = new SqlCommand(SQLView, SQLConnection); SqlDataAdapter DataAdapter = new SqlDataAdapter(SQLCommandCreateView); DataSet UserSet = new DataSet(); DataAdapter.Fill(UserSet); SQLCommandCreateView.Connection.Close(); string SQLQueryTop2 = "SELECT TOP 5 [Content_View].* " + "FROM [Content_View] "; SqlCommand SQLCommandTop2 = new SqlCommand(SQLQueryTop2,SQLConnection); SqlDataAdapter DataAdapterTop2 = new SqlDataAdapter(SQLCommandTop2); DataSet UserSetTop2 = new DataSet(); DataAdapterTop2.Fill(UserSetTop2); SQLCommandTop2.Connection.Close(); string SQLDropView = "DROP VIEW Content_View "; SqlCommand SqlDropView = new SqlCommand(SQLDropView,SQLConnection); SqlDropView.Connection.Open(); SqlDropView.ExecuteNonQuery(); SqlDropView.Connection.Close(); return UserSetTop2; } Pretty much using a view to sort then getting the top 5 from the view then deleting the view. Is there an easier way to do this? I am not sure the preformence hits on creating and droping views. or just a all around easier way to do this. thanks any points will be great

        B Offline
        B Offline
        Bill Dean
        wrote on last edited by
        #3

        Hi Jacob, I have a couple of fairly phylisophical points to add. Ignore them as you please :). First off, it's not a really good idea to change database schema from within your code. Creating and deleting view (and table and sprocs, etc) are really design-time processes. Do it before-hand and be done. Why? Well, what happens when user #2 tries to create the view before user #1 drops? You've got an exception to handle... It just get's too messy. Related to the first point, I am very leary of a program running with a sql user that has sufficient privileges to alter the db schema. Just my $0.02 Bill

        1 Reply Last reply
        0
        • M Mike Ellison

          public DataSet GetEntries()
          {
          string sql = "SELECT TOP 5 * FROM [HJ_BLog] ORDER BY Date DESC";
          string ConnectionString = ConfigurationSettings.AppSettings.Get("connString");
          SqlConnection conn;
          DataSet ds;
          SqlCommand cmd;
          SqlDataAdapter da;

          try
          {
          conn = new SqlConnection(ConnectionString);
          cmd = new SqlCommand(sql, conn);
          da = new SqlDataAdapter(cmd);
          ds = new DataSet();
          da.Fill(ds);
          }
          catch (Exception ex)
          {
          // do something...
          }
          finally
          {
          if (da != null) da.Dispose();
          if (cmd != null) cmd.Dispose();
          if (conn != null) conn.Dispose();
          }

          return ds;
          }

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

          Thank you, I have no idea what i was thinking when i wrote this.

          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