mysql 简单命令 启动服务器 net start mysql 停止服务器 net stop mysql
如何通过ip地址访问数据库 将mysql/表/user/Host的localhost改为“%” ; 重启mysql:计算机–》管理–》服务和应用程序–》服务–》mysql–》重新启动 或者在cmd使用命令 net start mysql net stop mysql
如何通过C#(.NET)进行数据库的连接
连接数据库
新建数据库 数据库名:**+db 字符集:utf8mb4
新建表 设置字段为不可重复 在索引 中选择字段,设置索引类型为UNIQUE 。
vs里面如何使用MySQL
依赖性–》右键 管理NuGet程序包–》搜索mysql–》安装MySQL.Data
访问数据库步骤
示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using MySql.Data.MySqlClient;namespace MysqlTest { class MainView { string connStr = "server=localhost;database=schoolsystemdb;uid=root;pwd=lczyyds;charset=utf8" ; public void Login () { Console.WriteLine("*********欢迎登录************" ); Console.Write("请输入账号:" ); String? account = Console.ReadLine(); Console.Write("请输入密码:" ); String? password = Console.ReadLine(); MySqlConnection conn = new MySqlConnection(connStr) ; conn.Open(); string sql = $"select * from users WHERE account=@account and `password`=@password" ; MySqlParameter pera1 = new MySqlParameter("@account" , account); MySqlParameter pera2 = new MySqlParameter("@password" , password); MySqlCommand command = new MySqlCommand(sql, conn); command.Parameters.Add(pera1); command.Parameters.Add(pera2); 4. 执行脚本,返回数据库游标对象 MySqlDataReader reader =command.ExecuteReader(); if (reader.Read()) { Console.WriteLine($"账号{reader["account" ]} ,姓名:{reader["name" ]} " ); } reader.Close(); conn.Close(); } public void Register () { Console.WriteLine("*********注册界面************" ); Console.Write("请输入账号:" ); String? account = Console.ReadLine(); Console.Write("请输入密码:" ); String? password = Console.ReadLine(); Console.Write("请输入姓名:" ); String? name = Console.ReadLine(); Console.Write("请输入年龄:" ); int ? age = int .Parse(Console.ReadLine()); MySqlConnection conn = new MySqlConnection("server=localhost;database=schoolsystemdb;uid=root;pwd=lczyyds;charset=utf8" ); conn.Open(); string sql = $@"insert into users(account,`password`,`name`,age) values('{account} ','{password} ','{name} ','{age} ')" ; MySqlCommand command = new MySqlCommand(sql, conn); int result = command.ExecuteNonQuery(); if (result > 0 ) { Console.WriteLine("添加成功" ); } else Console.WriteLine("添加失败" ); conn.Close(); } } }
如果报错 Field ‘userId’ doesn’t have a default value” 需要将userId设置为自增
防止SQL注入 万能密码 ‘ or 1=1#..