Connect to remote MySQL database with Visual C#

前端 未结 5 899
花落未央
花落未央 2021-01-20 19:39

I am trying to connect to a remote MySQL database using Visual C# 2008 Express Edition. Is there a way to connect using the editor, or do I have to code the connection manua

5条回答
  •  野性不改
    2021-01-20 20:17

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using MySql.Data.MySqlClient;
    namespace ConsoleApplication1
    {
        class Program
    
        {
    
    
    
    
            static void Main(string[] args)
            {
                Console.WriteLine("Welcome ...!");
                String conString = "SERVER = localhost; DATABASE = l2emu; User ID = root; PASSWORD = password;";
    
            MySqlConnection  connection = new MySqlConnection(conString);
    
                String command = "SELECT * FROM characters";
              MySqlCommand  cmd = new MySqlCommand(command,connection);
    MySqlDataReader reader;
    try
    {
        connection.Open();
        cmd.ExecuteNonQuery();
        reader = cmd.ExecuteReader();
        cmd.CommandType = System.Data.CommandType.Text;
        while (reader.Read() != false)
        {
    
            Console.WriteLine(reader["char_name"]);
            Console.WriteLine(reader["level"]);
    
        }
    
        Console.ReadLine();
    
    }
    catch (MySqlException MySqlError)
    {
        Console.WriteLine(MySqlError.Message);
    }
    
            }
        }
    }
    

    here is the example but you must download the mysql connector,

提交回复
热议问题