当前位置:网站首页>[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();
边栏推荐
- Notes de base sur les plans illusoires d'IA (triés en 10 000 mots)
- Eslint reports an error
- Blender多鏡頭(多機比特)切換
- Feature (5): how to organize information
- 【Unity3D】无法正确获取RectTransform的属性值导致计算出错
- Blender多镜头(多机位)切换
- Introduction and Principle notes of UE4 material
- [leetcode] sword finger offer 53 - I. find the number I in the sorted array
- 2837xd code generation module learning (3) -- IIC, ECAN, SCI, watchdog, ECAP modules
- Blender model import UE, collision settings
猜你喜欢

AutoCAD - layer Linetype

Illusion -- Animation blueprint, state machine production, character walking, running and jumping action

Eslint reports an error

Unreal material editor foundation - how to connect a basic material

Blender volume fog

【UE5】蓝图制作简单地雷教程

Blender石头雕刻

High level application of SQL statements in MySQL database (II)

Message mechanism -- getting to know messages and message queues for the first time

阿里云SLS日志服务
随机推荐
pytest学习--base
Ue5 - ai Pursuit (Blueprint, Behavior tree)
Spatial interpretation | comprehensive analysis of spatial structure of primary liver cancer
2.14 is it Valentine's day or Valentine's day when the mainstream market continues to fluctuate and wait for changes?
判断数组中是否存在重复元素
Pycaret | a few lines of code to solve machine learning modeling
[Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology
Leetcode -- the nearest common ancestor of 236 binary tree
Vscode auto format
Blender多鏡頭(多機比特)切換
UE illusory engine programmed plant generator setup -- how to quickly generate large forests
2021-09-12
Delivery mode design of Spartacus UI of SAP e-commerce cloud
【MySQL】连接MySQL时出现异常:Connection must be valid and open
两数之和,求目标值
2021-10-02
Blender多镜头(多机位)切换
About the college entrance examination
【Lua】常见知识点汇总(包含常见面试考点)
This article takes you to learn in detail what is fiber to home FTTH