(一)安装完MySQL后,打开Navicat进行连接,连接名随便输入,本地的(此电脑上的)MySQL主机就为:localhost或者127.0.0.1,用户名和密码:如果是本地MySQL就为你安装时设置的用户名和密码。
(二)(如果是本地MySQL)快速的创建如下表:
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using MySql.Data.MySqlClient;
-
- namespace ConsoleApp1
- {
- class Program
- {
- //建立连接
- public static MySqlConnection mycon;
- static void Main(string[] args)
- {
- string constr = "server=127.0.0.1;User Id=root;password=admin;Database=scores;charset=utf8";
- //建立连接
- mycon = new MySqlConnection(constr);
- //打开连接
- mycon.Open();
-
- bool isOK = mycon.Ping();
- if (isOK)
- {
- Console.WriteLine("数据库已连接");
- }
- else
- {
- Console.WriteLine("数据库连接错误");
- }
-
- //查询数据
- string selstr = "select * from studentscores";
- MySqlCommand myselect = new MySqlCommand(selstr, mycon);
-
- DataSet ds = new DataSet();
-
- try
- {
- MySqlDataAdapter da = new MySqlDataAdapter(selstr, mycon);
- da.Fill(ds);
- Console.WriteLine("数据库第一行数据:\n");
- for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
- {
- Console.WriteLine(ds.Tables[0].Rows[0][i]);
- }
- }
- catch (Exception e)
- {
- throw new Exception("SQL:" + selstr + "\n" + e.Message.ToString());
- }
-
-
- //修改数据
- MySqlCommand cmd = new MySqlCommand();
- try
- {
- cmd.Connection = mycon;
- cmd.CommandText = "UPDATE studentscores SET name = @name WHERE guid = @guid";
-
-
- SqlParameter name = new SqlParameter("@name", SqlDbType.VarChar, 255);
- name.Value = "C#Test";
- SqlParameter guid = new SqlParameter("@guid", SqlDbType.VarChar, 255);
- guid.Value = "1";
- cmd.Parameters.AddWithValue("@name",name.Value.ToString());
- cmd.Parameters.AddWithValue("@guid",guid.Value.ToString());
-
- cmd.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- mycon.Close();
- }
-
-
- Console.ReadKey();
- }
- }
- }
结果展示:
红框就是已经修改数据的内容
- using MySql.Data.MySqlClient;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Windows;
- using UnityEngine;
-
- public class TestMysql : MonoBehaviour
- {
- //建立连接语句
- //charset=utf8这句要写,不然可能会报错
- string constr = "server=127.0.0.1;User Id=root;password=admin;Database=scores;charset=utf8";
- //建立连接
- public static MySqlConnection mycon;
-
- void Start()
- {
- ConnectMysql();
- SearchMysql();
- UpadteMysql();
- }
-
- private void ConnectMysql()
- {
- string constr = "server=127.0.0.1;User Id=root;password=admin;Database=scores;charset=utf8";
- //建立连接
- mycon = new MySqlConnection(constr);
- //打开连接
- mycon.Open();
-
- bool isOK = mycon.Ping();
- if (isOK)
- {
- Debug.Log("数据库已连接");
- }
- else
- {
- Debug.Log("数据库连接错误");
- }
- }
- private void SearchMysql()
- {
- //查询数据
- string selstr = "select * from studentscores";
- MySqlCommand myselect = new MySqlCommand(selstr, mycon);
-
- DataSet ds = new DataSet();
-
- try
- {
- MySqlDataAdapter da = new MySqlDataAdapter(selstr, mycon);
- da.Fill(ds);
- Console.WriteLine("数据库第一行数据:\n");
- for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
- {
- Debug.Log(ds.Tables[0].Rows[0][i]);
- }
- }
- catch (Exception e)
- {
- throw new Exception("SQL:" + selstr + "\n" + e.Message.ToString());
- }
- }
- private void UpadteMysql()
- {
- //修改数据
- MySqlCommand cmd = new MySqlCommand();
- try
- {
- cmd.Connection = mycon;
- cmd.CommandText = "UPDATE studentscores SET name = @name WHERE guid = @guid";
-
-
- Debug.Log("取出guid=1的元组,更改属性为name=C#Test");
- String name = "C#Test";
- String guid = "1";
- cmd.Parameters.AddWithValue("@name", name);
- cmd.Parameters.AddWithValue("@guid", guid);
-
-
- cmd.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- //MessageBox.Show(ex.Message);
- throw new Exception(ex.Message.ToString());
- }
- finally
- {
- mycon.Close();
- }
- }
- }
再次强调注意:server=127.0.0.1;User Id=用户名;password=密码;Database=数据库;charset=utf8 (这里的用户名和密码都是你MySQL中的)
server=127.0.0.1;User Id=root;password=admin;Database=scores;charset=utf8 中的server如果为本机为localhost或者127.0.0.1(但是博主server=localhost程序出现了错误,所以建议用地址)!!!!!!!!localhost出错概率很大,数据库连接不上时一定将localhost更改成127.0.0.1再次尝试!