当前位置:网站首页>【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();
边栏推荐
- Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
- Junit5 supports suite methods
- Binary and decimal system of C language
- Career planning and development
- Personal experience & blog status
- ERROR 1118 (42000): Row size too large (> 8126)
- 2837xd 代码生成——StateFlow(2)
- How to judge the quality of primary market projects when the market is depressed?
- Project practice, redis cluster technology learning (12)
- Ue5 - AI pursuit (blueprint, behavior tree)
猜你喜欢
2837xd code generation - stateflow (3)
Vscode set JSON file to format automatically after saving
Applet development summary
2837xd code generation module learning (4) -- idle_ task、Simulink Coder
阿里云Prometheus监控服务
Blender体积雾
[illusory] automatic door blueprint notes
Personal experience & blog status
Kinect DK obtains color RGB images in cv:: mat format (used in openpose)
UE4 night lighting notes
随机推荐
阿里云Prometheus监控服务
Zlib download and use
How to achieve the top progress bar effect in background management projects
UE5——AI追逐(藍圖、行為樹)
[unreal] animation notes of the scene
[ue5] two implementation methods of AI random roaming blueprint (role blueprint and behavior tree)
【Unity3D】无法正确获取RectTransform的属性值导致计算出错
[Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology
职业规划和发展
虚幻——动画蓝图、状态机制作人物走跑跳动作
[illusory] automatic door blueprint notes
【虚幻4】UMG组件的简介与使用(更新中...)
Blender模型导入ue、碰撞设置
2837xd 代码生成——StateFlow(1)
Blender多镜头(多机位)切换
Brief analysis of edgedb architecture
【UE5】AI随机漫游蓝图两种实现方法(角色蓝图、行为树)
2837xd code generation - stateflow (1)
Career planning and development
Alibaba cloud Prometheus monitoring service