[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

c# mysql print some ting on screen

Posted on 2011-10-23
6
Medium Priority
?
256 Views
Last Modified: 2012-05-12
I can connect c# to mysql
But how can   select *  from a where colum a like 1
 I want to count the value of row and print the number of row on screen
0
Comment
Question by:teera
  • 3
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37014272
Using the MySqlConnector, which I assume you have since you say you can connect, you would simple create a connection object and a command object and execute your query:

using (MySqlConnection con = new MySqlConnection("your_connection_string"))
{
    using (MySqlCommand cmd = new MySqlCommand("select count(*)  from a where colum a like 1", con))
    {
        int count = (int)cmd.ExecuteScalar();

        textBox1.Text = count.ToString();
    }
}

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1400 total points
ID: 37014275
Heheh...  you should open the connection before you try to run the query!!

using (MySqlConnection con = new MySqlConnection("your_connection_string"))
{
    using (MySqlCommand cmd = new MySqlCommand("select count(*)  from a where colum a like 1", con))
    {
        try
        {
            con.Open();

            int count = (int)cmd.ExecuteScalar();

            textBox1.Text = count.ToString();
        }
        catch (MySqlException ex)
        {
             MessageBox.Show(ex.ToString());
        }
    }
}

Open in new window

0
 

Author Comment

by:teera
ID: 37014283
Hi kaufmed

if i want to print in console application how can i do it

Thank
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Assisted Solution

by:chrisbray
chrisbray earned 600 total points
ID: 37014302
Is this an SQL issue?  You have given no indication of the format of the table, nor how you determine that something is 'like' 1...

If you are trying to total values in a row you can add the columns together easily enough:

Select ColumnA+ColumnB+ColumnC as RowTotal
FROM MyTable

Open in new window


If you want to do it for a particular row then you can sort that with a where clause:

Select ColumnA+ColumnB+ColumnC as RowTotal
FROM MyTable
WHERE RowIdColumn = 1

Open in new window


Displaying it on the screen depends on how you bring it back from the database.  If you bring it back as a table, you can do something like this:

string totalAsText = table.Rows[0]["RowTotal"].ToString();

Other than that, if you can be more specific about your requirements I may be able to help more effectively.

Chris Bray
0
 
LVL 3

Expert Comment

by:chrisbray
ID: 37014307
In a console app, based on my earlier code:

Console.WriteLine(totalAsText);

Chris Bray
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37014325
Instead of

textBox1.Text = count.ToString();

Open in new window


you would use:

Console.WriteLine(count.ToString());

Open in new window


and instead of

MessageBox.Show(ex.ToString());

Open in new window


you would use:

Console.WriteLine(ex.ToString());

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question