当前位置:网站首页>【MySQL】连接MySQL时出现异常:Connection must be valid and open
【MySQL】连接MySQL时出现异常:Connection must be valid and open
2022-07-02 06:36:00 【趁着头发多我想做游戏】
今天尝试连接MySQL时出现了一个异常:
未经处理的异常: System.InvalidOperationException: Connection must be valid and open.
在 MySql.Data.MySqlClient.MySqlCommand.Throw(Exception ex)
在 MySql.Data.MySqlClient.MySqlCommand.CheckState()
在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()

这个异常是说连接必须是有效且开启的,但是奇怪,我明明已经调用了connection.Open()了啊?
经过检查,原来是这一句出现了问题:
MySqlCommand command = new MySqlCommand(commandString);
实际上构造函数中还需要传入MySqlConnection的对象,使用上述只有一个参数的构造方法会出现异常。
正确代码如下:
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();
边栏推荐
- ICLR 2022: how does AI recognize "things I haven't seen"?
- What wires are suitable for wiring on bread board?
- [illusory] weapon slot: pick up weapons
- How to handle error logic gracefully
- A model can do two things: image annotation and image reading Q & A. VQA accuracy is close to human level | demo can be played
- UE5——AI追逐(蓝图、行为树)
- UE5——AI追逐(藍圖、行為樹)
- Following nym, the new project Galaxy token announced by coinlist is gal
- Illusion -- Animation blueprint, state machine production, character walking, running and jumping action
- Matlab generates DSP program -- official routine learning (6)
猜你喜欢

Large neural networks may be beginning to realize: the chief scientist of openai leads to controversy, and everyone quarrels

Junit4运行mvn test 测试套件升级方案

Memories of a chat

2837xd 代码生成——StateFlow(1)

This article takes you to learn in detail what is fiber to home FTTH

Mixed development of uni app -- Taking wechat applet as an example

UE5——AI追逐(蓝图、行为树)

虚幻材质编辑器基础——如何连接一个最基本的材质

Matlab代码生成之SIL/PIL测试

How to judge the quality of primary market projects when the market is depressed?
随机推荐
Junit4运行mvn test 测试套件升级方案
Aiphacode is not a substitute for programmers, but a tool for developers
How to handle error logic gracefully
ue虚幻引擎程序化植物生成器设置——如何快速生成大片森林
MySQL index
Skywalking theory and Practice
MySQL -- time zone / connector / driver type
Network real-time video streaming based on OpenCV
Blender石头雕刻
Database -- acid of transaction -- introduction / explanation
A model can do two things: image annotation and image reading Q & A. VQA accuracy is close to human level | demo can be played
Error reporting on the first day of work (error reporting when Nessus installs WinPcap)
虚幻材质编辑器基础——如何连接一个最基本的材质
[200 Shengxin literatures] 95 multiomics exploration TNBC
Deep understanding of redis cache avalanche / cache breakdown / cache penetration
2837xd code generation module learning (1) -- GPIO module
go语言入门
[illusory] weapon slot: pick up weapons
2837xd代码生成模块学习(3)——IIC、eCAN、SCI、Watchdog、eCAP模块
Memories of a chat