当前位置:网站首页>[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();
边栏推荐
- Applet development summary
- 虚幻AI蓝图基础笔记(万字整理)
- pytest框架实现前后置
- 【JetBrain Rider】构建项目出现异常:未找到导入的项目“D:\VisualStudio2017\IDE\MSBuild\15.0\Bin\Roslyn\Microsoft.CSh
- [Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology
- Delivery mode design of Spartacus UI of SAP e-commerce cloud
- What is the relationship between realizing page watermarking and mutationobserver?
- 【UE5】AI随机漫游蓝图两种实现方法(角色蓝图、行为树)
- 2021-10-04
- 虚幻材质编辑器基础——如何连接一个最基本的材质
猜你喜欢

Mysql索引

The latest progress and development trend of 2022 intelligent voice technology

【MySQL】连接MySQL时出现异常:Connection must be valid and open

Sil/pil test of matlab code generation

Understand the composition of building energy-saving system

Blender模型导入ue、碰撞设置

pytest--之测试报告allure配置

Blender多镜头(多机位)切换

Configuration programmée du générateur de plantes du moteur illusoire UE - - Comment générer rapidement une grande forêt

2.14 is it Valentine's day or Valentine's day when the mainstream market continues to fluctuate and wait for changes?
随机推荐
pytest--之测试报告allure配置
【虚幻4】从U3D到UE4的转型之路
Metaclass type and using metaclass to implement model class ORM
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
Introduction et prévention des essais de pénétration
Ctrip starts mixed office. How can small and medium-sized enterprises achieve mixed office?
What is the relationship between realizing page watermarking and mutationobserver?
MySQL index
UE4 night lighting notes
Mysql索引
Operator exercises
go语言入门
Bookmark collection management software suspension reading and data migration between knowledge base and browser bookmarks
虛幻AI藍圖基礎筆記(萬字整理)
Alibaba cloud SLS log service
【虚幻】自动门蓝图笔记
Large neural networks may be beginning to realize: the chief scientist of openai leads to controversy, and everyone quarrels
2.14 is it Valentine's day or Valentine's day when the mainstream market continues to fluctuate and wait for changes?
The latest progress and development trend of 2022 intelligent voice technology
Alibaba cloud SMS service