如何在 C# 中显示 MySQL “PRINT” 命令的输出?
How Can i display the output of MySQL “PRINT” Command in C#?
我正在尝试使用 c# asp net 运行 mysql 存储过程。
在 mysql 中尝试过的这个存储过程工作正常。
而是在我的项目后面的代码上进行测试,我有 mysql 语法错误。
ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-8.0.12]You have an error
in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near 'mysp' at line 1
如何提取“PRINT”命令的输出,即 C# 中的过程,我该怎么做?
.cs
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
using (OdbcCommand command =
new OdbcCommand("mysp", cn))
{
try
{
command.Connection.Open();
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("sYear", ddlyear.SelectedValue);
using (OdbcDataAdapter sda = new OdbcDataAdapter(command))
{
DataTable dt = new DataTable();
sda.Fill(dt);
gv.DataSource = dt;
gv.DataBind();
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
sp
CREATE DEFINER=`root`@`%` PROCEDURE `mysp`(IN sYear VARCHAR(255))
BEGIN
DECLARE 2sYear VARCHAR(255);
SET 2sYear = sYear;
SET @s = CONCAT('SELECT * FROM `mytable_',2sYear,'`;');
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
编辑
这个问题在nbk用户的建议下解决了,原文有复制粘贴错误
谢谢
你的程序变量是 sYear
但是在你的代码中你使用了sAnno
CREATE TABLE mytable_2025 ( id int)
✓
CREATE PROCEDURE `mysp`(IN sYear VARCHAR(255))
BEGIN
DECLARE 2sYear VARCHAR(255);
SET 2sYear = sYear;
SET @s = CONCAT('SELECT * FROM `mytable_',2sYear,'`;');
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
✓
call mysp('2025')
| id |
| -: |
✓
SELECT @s
| @s |
| :---------------------------- |
| SELECT * FROM `mytable_2025`; |
SELECT @2sYear
| @2sYear |
| :------ |
| null |
db<>fiddle here
我正在尝试使用 c# asp net 运行 mysql 存储过程。
在 mysql 中尝试过的这个存储过程工作正常。
而是在我的项目后面的代码上进行测试,我有 mysql 语法错误。
ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-8.0.12]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysp' at line 1
如何提取“PRINT”命令的输出,即 C# 中的过程,我该怎么做?
.cs
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
using (OdbcCommand command =
new OdbcCommand("mysp", cn))
{
try
{
command.Connection.Open();
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("sYear", ddlyear.SelectedValue);
using (OdbcDataAdapter sda = new OdbcDataAdapter(command))
{
DataTable dt = new DataTable();
sda.Fill(dt);
gv.DataSource = dt;
gv.DataBind();
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
sp
CREATE DEFINER=`root`@`%` PROCEDURE `mysp`(IN sYear VARCHAR(255))
BEGIN
DECLARE 2sYear VARCHAR(255);
SET 2sYear = sYear;
SET @s = CONCAT('SELECT * FROM `mytable_',2sYear,'`;');
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
编辑
这个问题在nbk用户的建议下解决了,原文有复制粘贴错误 谢谢
你的程序变量是 sYear
但是在你的代码中你使用了sAnno
CREATE TABLE mytable_2025 ( id int)
✓
CREATE PROCEDURE `mysp`(IN sYear VARCHAR(255)) BEGIN DECLARE 2sYear VARCHAR(255); SET 2sYear = sYear; SET @s = CONCAT('SELECT * FROM `mytable_',2sYear,'`;'); PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt; END
✓
call mysp('2025')
| id | | -: | ✓
SELECT @s
| @s | | :---------------------------- | | SELECT * FROM `mytable_2025`; |
SELECT @2sYear
| @2sYear | | :------ | | null |
db<>fiddle here