Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 yertle, Today, 08:38 AM
                  6 responses
                  25 views
                  0 likes
                  Last Post ryjoga
                  by ryjoga
                   
                  Started by algospoke, Yesterday, 06:40 PM
                  2 responses
                  24 views
                  0 likes
                  Last Post algospoke  
                  Started by ghoul, Today, 06:02 PM
                  3 responses
                  16 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by jeronymite, 04-12-2024, 04:26 PM
                  3 responses
                  46 views
                  0 likes
                  Last Post jeronymite  
                  Started by Barry Milan, Yesterday, 10:35 PM
                  7 responses
                  23 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Working...
                  X