How to transmit parameters to SQL Server Stored Procedure using ODBC

Source: Internet
Author: User

The stored procedure is as follows:

Create procedure testsp
@ ID int
As
Return @ Error
Go

Online examples use SQL objects to transmit parameters, so they are written in the same format.CodeThe result cannot contain parameters. The Code is as follows:

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;
}

When the above Code runs to m_ccommand.executenonquery (); An exception occurs, saying that the parameter is not found. The problem lies in the commandtext attribute setting. The correct format should be as follows:

M_ccommand.commandtext = "{call testsp (?)} ";

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.