Heim  >  Artikel  >  Datenbank  >  使用ODBC向SQLServer存储过程传参数的方法

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

WBOY
WBOYOriginal
2016-06-07 15:40:161229Durchsuche

存储 过程 如下: 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(?)}";

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn