Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 7th, 2008, 04:31 PM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to naifwonder
Default C# and mySQL Connection

I have been working on a financial analysis program for some time now, and have run into some what of a snag. Before I delve into the details, I just want to state that I am fairly new at C# so please pardon any noob statements.

The program takes data from a given financial market, processes it (very small pieces of data.. a number such as 1192.75 would be the longest double to come in), then saves the output from the processing to a mySQL database. It does this very rapidly many times a second (usually around 5 to 10 times per second, although sometimes more).

Now here comes the problem. The output which the program saves in the database is something I need to be able to access in real-time. However, the program has about a 1 to 2 minute lag in saving the output to the database. I have identified the part of the program most likely creating the lag and am having difficulty fixing it.

The main class has two parts to it: One method loads when the program first initializes, and the next method runs whenever a new piece of data comes in (usually around 5-10 times per second). The problem is that in the second method, I have to reestablish a connection to the mySQL database every single time, then input the data. Is there anyway that I could establish a connection to the mySQL database in the first method and use that connection in the second method, instead of having to constantly reestablish a connection to the database several times per second? I think that being able to do so would significantly improve the performance of the program, and may help to eliminate the lag.

Reply With Quote
  #2 (permalink)  
Old March 7th, 2008, 06:30 PM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes you can, because the connection is passed by reference you can open it in the first method.

Code:
void MethodOne()
{
    SqlConnection conn = null;
    try
    {
       conn.Open();
       MethodTwo( conn );
    }
    finally
    {
        if( conn != null && conn.State == ConnectionState.Open() )
        {
           conn.Close();
        }
    }
}

void MethodTwo( SqlConnection conn )
{
   if( conn.State == ConnectionState.Open() )
   {
        // do stuff with connection 
   }
}
One important thing to remember not to keep the connection open any longer than you have to, but this will work.

I hope this answers your question.

Reply With Quote
  #3 (permalink)  
Old March 7th, 2008, 08:05 PM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Re-reading your original post, I think I misunderstood your question. I think you want this instead:
Code:
void MethodOne()
{
    SqlConnection conn = new SqlConnection( CONN_STRING );
    conn.Open();       
}

void MethodTwo()
{
   if( this.conn.State == ConnectionState.Open() )
   {
        // do stuff with connection 
   }
}
And depending on when you're done with it, close it.

Reply With Quote
  #4 (permalink)  
Old March 7th, 2008, 09:59 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Quote:
quote:Originally posted by panda869
 Re-reading your original post, I think I misunderstood your question. I think you want this instead:
Code:
void MethodOne()
{
    SqlConnection conn = new SqlConnection( CONN_STRING );
    conn.Open();       
}

void MethodTwo()
{
   if( this.conn.State == ConnectionState.Open() )
   {
        // do stuff with connection 
   }
}
And depending on when you're done with it, close it.

This example will not compile since conn is out of scope for MethodTwo.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #5 (permalink)  
Old March 8th, 2008, 12:59 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by dparsons
 
Quote:
quote:Originally posted by panda869
Quote:
 Re-reading your original post, I think I misunderstood your question. I think you want this instead:
Code:
void MethodOne()
{
    SqlConnection conn = new SqlConnection( CONN_STRING );
    conn.Open();       
}

void MethodTwo()
{
   if( this.conn.State == ConnectionState.Open() )
   {
        // do stuff with connection 
   }
}
And depending on when you're done with it, close it.

This example will not compile since conn is out of scope for MethodTwo.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Thanks for catching that, and sorry for posting incorrect info. That'll teach me to answer posts when I'm in a hurry.

Code:
SqlConnection instanceConn;

void MethodOne()
{
   instanceConn = new SqlConnection( CONN_STRING );
   instanceConn.Open();       
}

void MethodTwo()
{
   if( this.instanceConn.State == ConnectionState.Open() )
   {
        // do stuff with connection 
   }
}
Reply With Quote
  #6 (permalink)  
Old March 8th, 2008, 01:52 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

No worries. No one is perfect =]
-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP connection to MySQL ells228 Classic ASP Databases 5 June 21st, 2017 01:38 PM
Java MySql Connection ruzdi Java Basics 21 May 14th, 2012 12:57 AM
DataSource connection with mysql joychand Struts 0 April 24th, 2007 10:19 AM
MySQL Database Connection jayteee Dreamweaver (all versions) 0 December 11th, 2004 11:27 PM
Mysql connection catab C# 4 December 7th, 2003 11:25 AM



All times are GMT -4. The time now is 02:49 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.