Results 1 to 7 of 7

Thread: [RESOLVED] [2.0] there has to be a better way to do this.

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [RESOLVED] [2.0] there has to be a better way to do this.

    Hi all

    I want to make a sub that i send a Query and maybe some other variables (server location and table name) and i want it to return a datatable in a dataset that i can then feed into other subs for data manipulation as well as fill a DGV

    the question is , if i have the sub return as a dataset how can i do more than one thing with it with out having to re run the sub again and again.

    is it efficient to do something like "Dataset DS = subprocdure(query)" and then use DS in my code and if i do this wont it cause a duplication of data? (basicly ill have two containers sitting in memory)


    Here is my code ( and please PLEASE feel free to correct anything else you think should be improved)


    Ps yes i know the sub is names stupid thats just while im playing with it
    c# Code:
    1. private void button2_Click(object sender, EventArgs e)
    2.         {
    3.          string query = "SELECT gci_requests.id, gci_requests.ref_num, gci_requests.summary, gci_requests.description, gci_requests.open_date, gci_requests.last_modified_date, gci_requests.close_date, gci_requests.resolve_date, gci_requests.status, gci_requests.rootcause, gci_requests.priority, gci_requests.urgency, gci_requests.problem, gci_requests.type, gci_requests.category_name, gci_requests.assignee_adaccount, gci_requests.reportedby_adaccount, gci_requests.customer_adaccount, gci_requests.group_name FROM mdb_rpt.dbo.gci_requests gci_requests WHERE gci_requests.group_name='USAT-Publishing Solutions'";
    4.  
    5.          
    6.             dataGridView1.DataMember = "gci_requests";
    7.             dataGridView1.DataSource = Test(query); //<--- this is what i want to do better becuase i might need to use it somewhere else
    8.        
    9.         }
    10.         private DataSet Test(string query)
    11.         //worktodo
    12.             // add more prams for server and tablename
    13.             // improve catch to post error
    14.         {
    15.             DataSet ds = new DataSet();
    16.             SqlConnection conn = new SqlConnection("Server=gci-mocsqdb01;Database=mdb_rpt;Uid=caiahd_rpt;Pwd=dashboard;");//Driver={SQL Server};
    17.             try
    18.             {
    19.                 conn.Open();
    20.                 String SelectCmdString = query;
    21.                 SqlDataAdapter da = new SqlDataAdapter(SelectCmdString, conn);
    22.                 da.Fill(ds, "gci_requests");
    23.             }
    24.             catch
    25.             { MessageBox.Show("database connection Failed"); }
    26.             finally
    27.             {
    28.                 conn.Close();
    29.             }
    30.              return ds;
    31.         }
    Last edited by Crash893; Jul 11th, 2007 at 12:25 PM.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Arrow Re: [2.0] there has to be a better way to do this.

    Quote Originally Posted by Crash893
    Hi all

    I want to make a sub that i send a Query and maybe some other variables (server location and table name) and i want it to return a datatable in a dataset that i can then feed into other subs for data manipulation as well as fill a DGV

    the question is , if i have the sub return as a dataset how can i do more than one thing with it with out having to re run the sub again and again.

    is it efficient to do something like "Dataset DS = subprocdure(query)" and then use DS in my code and if i do this wont it cause a duplication of data? (basicly ill have two containers sitting in memory)


    Here is my code ( and please PLEASE feel free to correct anything else you think should be improved)


    Ps yes i know the sub is names stupid thats just while im playing with it
    c# Code:
    1. private void button2_Click(object sender, EventArgs e)
    2.         {
    3.           publisherObjectController = PublisherObjectController.Create();
    4.           publisherObjectController.Load();
    5.          }

    Put your Test method in a separate class. Rename the method to Load().

    Then you can truly use it anywhere.

    As far as calling the sub again and again:

    It comes down to how many times you expect to get the same data. If the data will stay the same between calls, you may want to load it once and cache it. If the data changes, there's no point in caching.
    Last edited by nemaroller; Jul 11th, 2007 at 01:19 PM.

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: [2.0] there has to be a better way to do this.

    im not sure i follow

    how do i make it a class?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] there has to be a better way to do this.

    Have your method take a DataSet as a parameter. Then you can create one DataSet outside the method and keep passing it in each time. That way you end up with only one DataSet containing multiple DataTables.

    Alternately you could forget the DataSet altogether. Do you actually need a DataSet? DataTables can happily exist on there own with no DataSet. If you don't need to create DataRelations between the tables then does a DataSet really serve a useful purpose?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: [2.0] there has to be a better way to do this.

    that is a great idea

    ( i probably dont but the idea is that in the future i might need to do releation work in the future so i might as well figure it out now)


    EDIT

    do i still need to Return the passed in Dataset? im a little fuzzy about this

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] there has to be a better way to do this.

    You already have a reference to the DataSet so why would you need another?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: [2.0] there has to be a better way to do this.

    Quote Originally Posted by jmcilhinney
    You already have a reference to the DataSet so why would you need another?
    well when you put it that way it does seem kind of obvious

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width