MySql.Data.MySqlClient.MySqlException: Parameter ‘@maxid’ must be defined
本文涉及到的mysql知识点:
- mysql中的if条件语句用法: IF(expr1,expr2,expr3)
- mysql使用变量(mysql中变量不用事前申明)
- mysql事务
- testcase
为了测试mysql事务,写了个testcase,
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data; [TestClass]
public class MySqlTransactionTest
{
[TestMethod]
public void AddStudent()
{
string sql = @"
BEGIN;
SELECT @maxid:= MAX(id) FROM student;
SET @maxid:=IF(@maxid IS NULL,0,@maxid);
INSERT student VALUES(@maxid+1,'');
COMMIT;";
ExecuteNonQuery(sql); } string Connstring = "server=192.168.40.223;user id=yimei;password=password;database=test;Character Set=utf8;";
private int ExecuteNonQuery(string sql)
{
MySqlConnection conn = new MySqlConnection(Connstring);
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
//Console.WriteLine("ExecuteNonQuery:{0}", i);
return i;
}
}
运行testcase,发现报如下异常:MySql.Data.MySqlClient.MySqlException: Parameter ‘@maxid’ must be defined

看来,是ado.net把@maxid当成了一个变量,要求必须传递@maxId参数。 当然,这违背了我的初衷——我的sql逻辑是在新增student时将其id字段值设置为现有最大id+1,我本来只是希望它把我这段sql执行一下即可。
和一个哥们讨论了一下,他说如果ado.net连接的是SqlServer,这种情况是没有问题的,我将信将疑。不过,经过测试,还真是如此。
那么,mysql怎么就会报异常呢?
我的mysql版本是(命令SELECT @@VERSION;):5.5.50-MariaDB。网上了解了一下,要在连接串里显示的加一个Allow User Variables=True,才能允许sql里有用户自定义变量的出现(见老外的博客http://blog.tjitjing.com/index.php/2009/05/mysqldatamysqlclientmysqlexception-parameter-id-must-be-defined.html)。
附ado.net连接SqlServer场景用例:
using System.Data.SqlClient;
[TestMethod]
public void MsSqlAt()
{
string sqlConnstring = "server=192.168.10.27;user id=sa;password=En7Jw5Xh;database=sms;";
string sql = @"
declare @num int;
SELECT @num=count(1) from T_sms_send;
select @num;
";
using (SqlConnection conn = new SqlConnection(sqlConnstring))
{
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
var obj = cmd.ExecuteScalar();
conn.Close();
Console.WriteLine(obj);
}
}
MySql.Data.MySqlClient.MySqlException: Parameter ‘@maxid’ must be defined的更多相关文章
- MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
转自:https://blog.csdn.net/zhaoqi5705/article/details/12087649?locationNum=15 MySql.Data.MySqlClient.M ...
- 错误记录-MySql.Data.MySqlClient.MySqlException (0x80004005): Timeout expired.
-- ::25.026 +: [ERR] Connection id "0HLQH64H76UL5", Request id "0HLQH64H76UL5:0000000 ...
- MySql 8.0 C#连接报错 MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to host '12.118.224.181' for user 'root' using method 'caching_sha2_password' failed with message: Reading from t
解决方法 在连接字符串后面加上 SslMode=None
- at MySql.Data.MySqlClient.MySqlStream.ReadPacket 或 FUNCTION account.AddMinutes does not exist
Application Exception MySql.Data.MySqlClient.MySqlException FUNCTION account.AddMinutes does not exi ...
- C#工具类MySqlHelper,基于MySql.Data.MySqlClient封装
源码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- 无法为具有固定名称“MySql.Data.MySqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类型“MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6”
"System.InvalidOperationException"类型的未经处理的异常在 mscorlib.dll 中发生 其他信息: 无法为具有固定名称"MySql. ...
- MySql.Data.MySqlClient连接MySql
在C#中连接MySql数据库其实是件很简单的事情,但对于刚开始学习C#的朋友来说,问题却是不小,主要原因是相对于ACCESS和MSSql来说,MySql方面的教程文章实在太少,我也是自己摸索好好半天才 ...
- QA:无法为具有固定名称“MySql.Data.MySqlClient”...
Question: 无法为具有固定名称“MySql.Data.MySqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类型“MySql.Data.MySqlC ...
- System.TypeInitializationException: 'The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception.'
下午在调试的时候报错数据库连接就报错我就很纳闷后面用原来的代码写发现还是报错 System.TypeInitializationException: 'The type initializer for ...
随机推荐
- 控制台游戏引擎CGE——贪吃蛇
今天我也来发一个控制台游戏.先看图: 缘起 LZ是一个有严重拖延症的人,表现的形式就是隔一段时间就要刷一刷博客园. 这不前几天,看到了魏大师<使用Lua脚本语言开发出高扩展性的系统...> ...
- 从vue1.0到vue2.0遇到的一些问题的记录
1.取消v-el的使用方式,改为refs使用 获取指定的dom元素 html:<div ref="div"></div> js: $refs.div 问题: ...
- BZOJ2342 Manacher + set
题一:别人介绍的一道题,题意是给出一个序列,我们要求出一段最常的连续子序列,满足:该子序列能够被平分为三段,第一段和第二段形成回文串,第二段和第三段形成回文串. 题二:BZOJ2342和这题非常的相似 ...
- Hdu4311 || 4312Meeting point-1/-2 n个点中任意选一个点使得其余点到该点曼哈顿距离之和最小
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- ASP.NET中GridView数据导出到Excel
/// <summary> /// 导出按钮 /// </summary> /// <param name="sender"></para ...
- LeetCode 412. Fizz Buzz
Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...
- Mac锁屏
http://www.dbform.com/html/2006/192.html 应用程序-实用工具-钥匙锁-菜单栏中的钥匙串访问-偏好设置-选中“在菜单栏中显示钥匙串”
- tornado 学习笔记17 HTTPServerRequest分析
代表Http请求. 所有的属性都是字符串型. 17.1 属性 (1) method:请求方法类型,比如"GET"."POST" (2) ur ...
- java执行linux命令
package com.gtstar.collector; import java.io.BufferedReader;import java.io.IOException;import java.i ...
- Django models知识小点
django 为使用一种新的方式,即关系对象映射(ORM) 一,创建表 1,基本结构 注意: 1,创建标的时候,如果我们不给表加自增列,生成表的时候会默认给我们生成一列为ID的自增列,当然我们也可以自 ...