当前位置:网站首页>[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();
边栏推荐
- What is the relationship between realizing page watermarking and mutationobserver?
- 2021-09-12
- Introduction and prevention of penetration test
- Basic notes of illusory AI blueprint (10000 words)
- [ue5] two implementation methods of AI random roaming blueprint (role blueprint and behavior tree)
- 2021-10-04
- Determine whether there are duplicate elements in the array
- A model can do two things: image annotation and image reading Q & A. VQA accuracy is close to human level | demo can be played
- AutoCAD - layer Linetype
- 虚幻——动画蓝图、状态机制作人物走跑跳动作
猜你喜欢
随机推荐
What is call / cc- What is call/cc?
Junit4 runs MVN test test suite upgrade scheme
Introduction et prévention des essais de pénétration
【UE5】动画重定向:如何将幻塔人物导入进游戏玩耍
【虚幻4】从U3D到UE4的转型之路
Blender volume fog
Project practice, redis cluster technology learning (6)
Blender石头雕刻
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
ERROR 1118 (42000): Row size too large (> 8126)
2021-09-12
[unreal] key to open the door blueprint notes
Unreal material editor foundation - how to connect a basic material
Ctrip starts mixed office. How can small and medium-sized enterprises achieve mixed office?
Applet development summary
Project practice, redis cluster technology learning (10)
Leetcode -- the nearest common ancestor of 236 binary tree
2021-09-12
[ue5] two implementation methods of AI random roaming blueprint (role blueprint and behavior tree)
What wires are suitable for wiring on bread board?