• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Problem with connecting to a local MySQL server through C#

VinylxScratches

Golden Member
I'm trying to write program that connects to a MySQL and does a select query.

I can connect to the server but it doesn't return anything.

Here is my code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

MySqlConnection MyCon = new MySqlConnection("server=localhost;database=test2;uid=root;password=123456");

MySqlConnection connection = MyCon;
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "SELECT * FROM car;";
connection.Open();
Reader = command.ExecuteReader();

while(Reader.Read())
{
Console.Write("Record Found");
}

Reader.Close();
connection.Close();

}
}
}

I know it connects correctly because if I change the user name or anything from the connectionstring, the program will crash.

My MySQL DB is just called test2
table is called cars with columns

ID and Name

If I do a select query in MySQL query program I get this.

ID, Name
1, Dodge
2, Chevy

I know my app doesn't display data but I was hoping to get 2 rows saying "Record Found" at least.
 
Have you tried setting a breakpoint and evaluating the values of the reader object after it populates it with that select statement?
 
The very bottom line says the enumeration yielded no results.

I just copied and pasted your code into a console app of my own and got it to work. Same references, slightly different dbname and table name. I added in a '+ "/r/n"' just to make it start a new line and it wrote 2 "Record found" to the console window.
 
the number of columns in the table. So it's reading the table it seems but it's not getting any results.
 
nvm about that enumeration thing, I had the same message. The HasRows boolean value is true for me though. It's false for you.
 
Originally posted by: VinylxScratches
Hmm, I don't know why I'm having issues.... Did you have to do anything in MySQL to get it to work with Visual Studio? I'm out of ideas.

Nope. I just installed mysql and the .net connector files. Then I copied your code, made some slight changes(db name, table name, and had it start a new line) and opened the console app in debug mode and it output what it was supposed to output.

I would create a new db from scratch, recreate the table, and populate the table with some records, then give the console app another try with the new db and table.
 
Originally posted by: VinylxScratches
Well I'm embarrassed. my table was called cars, not car :-x

Thanks
lol. I was just going to post that... your SQL said "car" and your description below mentioned "cars". 😉
 
Originally posted by: clamum
Originally posted by: VinylxScratches
Well I'm embarrassed. my table was called cars, not car :-x

Thanks
lol. I was just going to post that... your SQL said "car" and your description below mentioned "cars". 😉

I missed that myself. :laugh:
 
Back
Top