Mysql Database-server connection
Before using this code in C# for connection you have to need two libraries.
These libraries should be included to use mysql() functions.
Code:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
MySql.Data.MySqlClient;
namespace
MysqlConsoleConn
{
class Program
{
static void Main(string[] args)
{
string connstring =
"server=localhost;Database=login;Uid=root;password=;";
MySqlConnection conn = new
MySqlConnection(connstring);
MySqlCommand cmd =
conn.CreateCommand();
cmd.CommandText = "select *
from login";
try
{
conn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
MySqlDataReader reader =
cmd.ExecuteReader();
while (reader.Read()) {
Console.WriteLine(reader["username"].ToString());
Console.WriteLine(reader["password"].ToString());
}
Console.ReadLine();
}
}
}
|