• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

MySQL & Ninja

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    MySQL & Ninja

    This works perfectly:

    PHP Code:
    protected override void OnBarUpdate()
    {
    //create a MySQL connection 
    MySqlConnection connection = new MySqlConnection("Server=server;Database=database;Port=port;Uid=userid;Password=password"); 

    //create a MySQL insert command with a query string 
    string myInsertQuery String.Format ("INSERT INTO table (column1, column2) values ('value1', 'value2')"); 
    MySqlCommand command = new MySqlCommand(myInsertQuery); 

    command.Connection connection
    connection.Open(); 

    //insert info 
    command.ExecuteNonQuery(); 
                     
    //close the connection 
    connection.Close(); 

    This results in "Connection must be valid and open" or in Termination fail if I move connection.Open() to OnBarUpdate:

    PHP Code:
    protected override void OnStartUp()
    {
    //create a MySQL connection 
    MySqlConnection connection = new MySqlConnection("Server=server;Database=database;Port=port;Uid=userid;Password=password"); 

    connection.Open(); 

    Anyone know why? I tried a Print statement in OnStartUp and it printed just fine.
    Last edited by dsraider; 01-30-2012, 03:26 PM.

    #2
    Edited for new purpose.

    Comment


      #3
      dsraider,

      Unfortunately this level of coding is unsupported. Hopefully a community member can help you.

      Please let us know if we can assist further.
      Adam P.NinjaTrader Customer Service

      Comment


        #4
        Hi Adam,

        Unfortunately, I kind of figured that. However, if you're aware of any issues at all with OnStartup() or OnTermination(), please let me know.

        Thanks,
        DS

        Comment


          #5
          In our .net with oracle database and webpages, a sql connection must be opened every single time.

          This might be what is happening here to some degree.?.

          That's my guess. Now I have no idea why we are mixing .net with Oracle database at work. Bunch of no brains there.


          Originally posted by dsraider View Post
          This works perfectly:

          PHP Code:
          protected override void OnBarUpdate()
          {
          //create a MySQL connection 
          MySqlConnection connection = new MySqlConnection("Server=server;Database=database;Port=port;Uid=userid;Password=password"); 

          //create a MySQL insert command with a query string 
          string myInsertQuery String.Format ("INSERT INTO table (column1, column2) values ('value1', 'value2')"); 
          MySqlCommand command = new MySqlCommand(myInsertQuery); 

          command.Connection connection
          connection.Open(); 

          //insert info 
          command.ExecuteNonQuery(); 
                           
          //close the connection 
          connection.Close(); 

          This results in "Connection must be valid and open" or in Termination fail if I move connection.Open() to OnBarUpdate:

          PHP Code:
          protected override void OnStartUp()
          {
          //create a MySQL connection 
          MySqlConnection connection = new MySqlConnection("Server=server;Database=database;Port=port;Uid=userid;Password=password"); 

          connection.Open(); 

          Anyone know why? I tried a Print statement in OnStartUp and it printed just fine.

          Comment


            #6
            LOL sorry to hear about work but I hope I don't have to do this for every tick. In fact, I think my computer just started crying.

            Comment


              #7
              Has this issue been solved, I would also be interested in moving the OPEN and CLOSE part to startup and termination areas of ninjatrader code

              Comment


                #8
                I would also been interesting moving the OPEN and connect and CLOSE to startup and termination parts of ninjatrader code, any solutions here?

                Comment


                  #9
                  Not sure what happened originally.
                  But, looking at the code posted, when he moved the connection definition and open call to the OnStartUp method, he defined the connection object as a local variable.
                  So, that meant once OnStartUp exited the connection object was disposed.

                  Try defining the connection object in the Variables #region so it'll will persist for all methods.

                  Technically, as was mentioned before, you should establish a new connection with each unit of work so that you don't leave resources hanging.
                  BUt, given that this is a local DB, and probably single user, the best thing is to establish the connection on start up and then close it on termination.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Againmain1, Today, 03:04 AM
                  1 response
                  23 views
                  1 like
                  Last Post NinjaTrader_ShawnB  
                  Started by jmahon, Yesterday, 11:44 PM
                  1 response
                  16 views
                  0 likes
                  Last Post NinjaTrader_ChrisL  
                  Started by Tomhgriff1, 01-03-2020, 04:07 AM
                  4 responses
                  47 views
                  0 likes
                  Last Post bkonia
                  by bkonia
                   
                  Started by cary1267, Today, 09:37 AM
                  1 response
                  23 views
                  0 likes
                  Last Post NinjaTrader_ShawnB  
                  Started by nfsolutions, Today, 06:41 AM
                  1 response
                  25 views
                  0 likes
                  Last Post NinjaTrader_ShawnB  
                  Working...
                  X