I'm trying to link mysql in c#, but I don't know what the problem is.

Asked 2 years ago, Updated 2 years ago, 42 views

When compiling the Insert function, it doesn't seem to be open.

The select function causes a compilation error.

What's the problem?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Data;

namespace DB_test
{
    class Program
    {
        private static void SelectUsingAdapter()
        {
            DataSet ds = new DataSet();
            string connStr = "server=localhost;Uid=root; password=qwe123; database=db_test";

            using (MySqlConnection conn = new MySqlConnection(connStr))
            {
                //Using the MySqlDataAdapter class
                //Import data in disconnected mode
                string sql = "SELECT * FROM members";
                MySqlDataAdapter adpt = new MySqlDataAdapter(sql, conn);
                adpt.Fill(ds, "members");
            }

            foreach (DataRow r in ds.Tables[0].Rows)
            {
                Console.WriteLine(r["pwd"]);
            }
        }

        private static void Insert()
        {
            string connStr = String.Format("server=localhost;Uid=root; password=qwe123; database=db_test");
            MySqlConnection conn = new MySqlConnection(connStr);
            conn = new MySqlConnection(connStr);

            try
            {
                conn.Open();
                Console.WriteLine ("MySQL DB Connection");

            String sql = "INSERT INTO members (id, pwd, name) " +
                    "VALUES ('gon', '111', 'Kim Satgat');"

            MySqlCommand cmd = new MySqlCommand(sql, conn);
            cmd.ExecuteNonQuery();




            }
            catch { }
            finally
            {
                conn.Close();
                Console.WriteLine ("Close MySQL DB Connection");

            }
        }

        static void Main(string[] args)
        {

            Insert();
            //SelectUsingAdapter();

        }

    }
}

c# mysql

2022-09-22 21:53

1 Answers

Is there a compilation error instead of a runtime error that occurs after it is executed? Does it mean that there's a red line in the visual studio?

If it's a compilation error, wouldn't it be that you can't get the MySql library? Based on Visual Studio 2015, add MySql.Data in the top menu Project-Add Reference window.

Runtime error, not compile error. So if the value you want is not printed out, I'll do the following and you'll get the value.

private static void SelectUsingAdapter()
{
    DataSet ds = new DataSet();
    string connStr = "server=localhost;Uid=root; password=???; database=test";
    MySqlConnection conn = null;
    MySqlDataReader rdr = null;
    try
    {
        conn = new MySqlConnection(connStr);
        conn.Open();
        string stm = "SELECT * FROM my_table";
        MySqlCommand cmd = new MySqlCommand(stm, conn);
        rdr = cmd.ExecuteReader();

        while (rdr.Read())
        {
            Console.WriteLine(rdr.GetString(1));
        }
    }
    catch(Exception e)
    {

    }
    finally
    {
        if (conn != null)
        {
            conn.Close();
        }
    }

}

Instead of GetString (1), you can write down the number of columns you want, and change the table name in the select statement.


2022-09-22 21:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.