Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 7th, 2008, 04:31 PM
Registered User
 
Join Date: Mar 2008
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.

 
Old March 7th, 2008, 06:30 PM
Registered User
 
Join Date: Mar 2008
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.

 
Old March 7th, 2008, 08:05 PM
Registered User
 
Join Date: Mar 2008
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.

 
Old March 7th, 2008, 09:59 PM
Wrox Author
 
Join Date: Oct 2005
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
================================================== =========
 
Old March 8th, 2008, 12:59 AM
Registered User
 
Join Date: Mar 2008
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 
   }
}
 
Old March 8th, 2008, 01:52 PM
Wrox Author
 
Join Date: Oct 2005
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
================================================== =========





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





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