How to extract data on the fly

Last post 02-02-2007 10:30 AM by ASQ. 1 replies.

Sort Posts:

  • How to extract data on the fly

    02-02-2007, 9:45 AM
    • Loading...
    • ASQ
    • Joined on 10-26-2006, 5:07 AM
    • Modimolle, SA
    • Posts 101

    Hi,

    I would like to extract data from an MySQL database table in my Software (on the fly). I have found the following example of how to add data to a table in Software and it works fine.  

    // specify the data source
        string connContStr = ConfigurationManager.ConnectionStrings["my_connection1"].ConnectionString;
        SqlConnection myContConn = new SqlConnection(connContStr);
    
    // define the command query
       String myContQuery = "INSERT INTO ContactUs (contType, contName, contEmail, contTel, contMessage, contDate) VALUES (@contType, @contName, @contEmail, @contTel, @contMessage, @contDate)";
       SqlCommand myContCommand = new SqlCommand(myContQuery, myContConn);
       myContCommand.Parameters.Add(new SqlParameter("@contType", dropdownType.Text));
       myContCommand.Parameters.Add(new SqlParameter("@contName", txtName.Text));
       myContCommand.Parameters.Add(new SqlParameter("@contEmail", txtEmail.Text));
       myContCommand.Parameters.Add(new SqlParameter("@contTel", txtTel.Text));
       myContCommand.Parameters.Add(new SqlParameter("@contMessage", txtMessage.Text));
       myContCommand.Parameters.Add(new SqlParameter("@contDate", DateTime.Now));
       myContConn.Open();
       myContCommand.ExecuteNonQuery();
       myContConn.Close();

    Can anyone point me to an example of how to extract data in a similar fashion? (I know that the "INSERT INTO..." should change to something like "SELECT [contName], [contEmail] FROM [ContactUs]".

    Also, how will the syntax look to display the information from say "contName" in a Label?

    Label.Text = ??

    Thanks for your feedback.

    Regards

    Jan

  • Re: How to extract data on the fly

    02-02-2007, 10:30 AM
    Answer
    • Loading...
    • ASQ
    • Joined on 10-26-2006, 5:07 AM
    • Modimolle, SA
    • Posts 101

    Hi,

    I managed to solve this issue myself. Should anyone be interested, here is what I did:

    Include these namespaces:

    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>

    As for the code:

    // specify the data source
       string connContStr = ConfigurationManager.ConnectionStrings["businessConnect"].ConnectionString;
       SqlConnection myConn = new SqlConnection(connContStr);
            
    // define the command query
       String query = "SELECT contName, contEmail, contTel, contMessage FROM ContactUs";
       SqlCommand myCommand = new SqlCommand (query, myConn);
            
    // open the connection and instantiate a datareader
       myConn.Open ();
       SqlDataReader myReader = myCommand.ExecuteReader();
            
    // loop thru the reader
        while (myReader.Read()) 
        {
           copyLABEL.Text = myReader.GetString(0);
           page1LINK.Text = myReader.GetString(1);
           page2LINK.Text = myReader.GetString(2);
           Page.Title = myReader.GetString(3);
         }
           
    // close the reader and the connection
         myReader.Close();
         myConn.Close(); 

    Regards

    Jan

Page 1 of 1 (2 items)
Microsoft Communities
Page view counter