当前位置:网站首页>[MySQL] an exception occurs when connecting to MySQL: connection must be valid and open
[MySQL] an exception occurs when connecting to MySQL: connection must be valid and open
2022-07-02 10:23:00 【I want to play games while my hair is heavy】
Try connecting today MySQL An exception occurred during :
Unhandled exception : System.InvalidOperationException: Connection must be valid and open.
stay MySql.Data.MySqlClient.MySqlCommand.Throw(Exception ex)
stay MySql.Data.MySqlClient.MySqlCommand.CheckState()
stay MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
stay MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
This exception means that the connection must be valid and open , But strange , I have already called connection.Open() ah ?
After inspection , It turns out that there is a problem with this sentence :
MySqlCommand command = new MySqlCommand(commandString);
In fact, the constructor also needs to be passed in MySqlConnection The object of , Using the above construction method with only one parameter will cause exceptions .
The correct code is as follows :
string connectString = "Persist Security Info=False;Username=root;Password=123456;" +
"database=MMOG;server=localhost;Connect Timeout=30";
// string connectString = "server=localhost;port=3306;user=root;password=123456; database=MMOG;";
// string connectString = "Database = MMOG;Data Source=127.0.0.1;port=3306;User Id=root;password=123456;";
MySqlConnection connection = new MySqlConnection(connectString);
connection.Open();
string commandString = "insert into `playerdata`(`username`,`password`) values(@username,@password)";
MySqlCommand command = new MySqlCommand(commandString,connection);
command.Parameters.AddWithValue("username", "player1");
command.Parameters.AddWithValue("password", "player1");
command.ExecuteReader();
边栏推荐
- [ue5] blueprint making simple mine tutorial
- Blender model import UE, collision settings
- [IDL] Research
- Project practice, redis cluster technology learning (10)
- Commutateur Multi - lentilles Blender
- Introduction to go language
- How to judge the quality of primary market projects when the market is depressed?
- Blender camera surround motion, animation rendering, video synthesis
- About the college entrance examination
- 【避坑指南】使用UGUI遇到的坑:Text组件无法首行缩进两格
猜你喜欢
Blender多镜头(多机位)切换
How to achieve the top progress bar effect in background management projects
【MySQL】连接MySQL时出现异常:Connection must be valid and open
What wires are suitable for wiring on bread board?
Matlab generates DSP program -- official routine learning (6)
【虚幻】自动门蓝图笔记
Configuration programmée du générateur de plantes du moteur illusoire UE - - Comment générer rapidement une grande forêt
VLAN experiment
Ue5 - ai Pursuit (Blueprint, Behavior tree)
Application of rxjs operator withlatestfrom in Spartacus UI of SAP e-commerce cloud
随机推荐
pytest--之测试报告allure配置
UE5——AI追逐(蓝图、行为树)
【避坑指南】Unity3D项目接入腾讯Bugly工具时遇到的坑
Deep understanding of redis cache avalanche / cache breakdown / cache penetration
Sum the two numbers to find the target value
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
Aiphacode is not a substitute for programmers, but a tool for developers
Alibaba cloud SMS service
How to achieve the top progress bar effect in background management projects
网络通信学习
ue4材质的入门和原理笔记
ERROR 1118 (42000): Row size too large (> 8126)
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
2837xd code generation module learning (3) -- IIC, ECAN, SCI, watchdog, ECAP modules
Notes de base sur les plans illusoires d'IA (triés en 10 000 mots)
Spatial interpretation | comprehensive analysis of spatial structure of primary liver cancer
Delivery mode design of Spartacus UI of SAP e-commerce cloud
2.14 is it Valentine's day or Valentine's day when the mainstream market continues to fluctuate and wait for changes?
Blender模型导入ue、碰撞设置
Project practice, redis cluster technology learning (VIII)