Home > Database > Mysql Tutorial > body text

使用ODBC向SQLServer存储过程传参数的方法

WBOY
Release: 2016-06-07 15:40:16
Original
1227 people have browsed it

存储 过程 如下: CREATEPROCEDURETestSP @IDint AS RETURN@@ERROR GO 网上的例子都是用Sql的对象来传 参数 的,于是 使用 相同的格式写了代码,结果不能传 参数 ,代码如下: try { OdbcConnectionm_cConn=newOdbcConnection(); OdbcCommandm_cCommand=newO

存储过程如下:

CREATE PROCEDURE TestSP
@ID int
AS
RETURN @@ERROR
GO

网上的例子都是用Sql的对象来传参数的,于是使用相同的格式写了代码,结果不能传参数,代码如下:

try
{
OdbcConnection m_cConn = new OdbcConnection();
OdbcCommand m_cCommand = new OdbcCommand();
OdbcParameter cParam = new OdbcParameter("@ID", OdbcType.Int, 4);

m_cConn.ConnectionString = "Dsn=MySQLServer;trusted_connection=Yes;wsid=BINARYTREE;database=SimpleProjectDB";
m_cConn.Open();
m_cCommand.Connection = m_cConn;
m_cCommand.CommandText = "TestSP";
m_cCommand.CommandType = CommandType.StoredProcedure;

m_cCommand.Parameters.Add(cParam);
m_cCommand.ExecuteNonQuery();
}
catch (Exception cEx)
{
return;
}

上面的代码运行到m_cCommand.ExecuteNonQuery(); 时出异常,说是参数没有找到,问题出在CommandText属性的设置上。正确的格式应该如下:

m_cCommand.CommandText = "{CALL TestSP(?)}";

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!