Sybase ase ado. net output parameters

Source: Internet
Author: User
Tags sybase
I don't know how many people use Sybase for database development. With Sybase ase ado. net, the system is running normally after a lot of resistance is broken through. Further efforts should be made to increase data permissions. We used to judge using the result set directly returned by the function on SQL Server. However, Sybase does not support functions, so we have to use the stored procedure to return a condition string for permission determination for dynamic execution. The problem is that the output parameter always prompts "command has been closed ". It's strange that the connection was opened during execution. Write a test Program :

Aseconnection con = new aseconnection ("Data Source = 'sybase '; Port = 5000; uid = 'sa'; Pwd =''; database = 'data '; connection timeout = '000000 ';");

Asecommand COM = new asecommand ("getdatarightsql", con );

Com. commandtype = system. Data. commandtype. storedprocedure;

Try

{

Aseparameter PRM = new aseparameter ("@ userid", 1 );

// PRM. Direction = system. Data. parameterdirection. input;

Com. Parameters. Add (PRM );

PRM = new aseparameter ("@ category", "Department ");

Com. Parameters. Add (PRM );

Com. Parameters. Add (New aseparameter ("@ fieldname", "dept_id "));

PRM = new aseparameter ("@ returnsql", asedbtype. varchar, 250 );

PRM. Direction = system. Data. parameterdirection. output;

Com. Parameters. Add (PRM );

Con. open ();

Com. executenonquery ();

Console. writeline (COM. Parameters ["@ returnsql"]. value );



}

Finally

{

If (con! = NULL & con. State = system. Data. connectionstate. open)

Con. Close ();

}

Return;

Ft. No problem. The returned results are displayed. The data is correct.

Because we have a data layer dedicated to database operations, we began to suspect that the encapsulation is not good. After checking for n times, I don't know where the error occurred. I thought there was a problem with the output parameter type. I tried several times and changed the length. No.

Later, I noticed that the trace execution was successful, and errors always occurred when taking the parameters. Let's take a look at the error message "command has been closed! The database connection is not required to be open when the parameter value is obtained, right?

Modify Test Code :

Aseconnection con = new aseconnection ("Data Source = 'sybase '; Port = 5000; uid = 'sa'; Pwd =''; database = 'data '; connection timeout = '000000 ';");

Asecommand COM = new asecommand ("getdatarightsql", con );

Com. commandtype = system. Data. commandtype. storedprocedure;

Try

{

Aseparameter PRM = new aseparameter ("@ userid", 1 );

// PRM. Direction = system. Data. parameterdirection. input;

Com. Parameters. Add (PRM );

PRM = new aseparameter ("@ category", "Department ");

Com. Parameters. Add (PRM );

Com. Parameters. Add (New aseparameter ("@ fieldname", "dept_id "));

PRM = new aseparameter ("@ returnsql", asedbtype. varchar, 250 );

PRM. Direction = system. Data. parameterdirection. output;

Com. Parameters. Add (PRM );

Con. open ();

Com. executenonquery ();

Con. Close (); // close the database in advance

Console. writeline (COM. Parameters ["@ returnsql"]. value );



}

Finally

{

If (con! = NULL & con. State = system. Data. connectionstate. open)

Con. Close ();

}

Return;

Haha, an error occurred.

Previously, when operating on SQL Server and Oracle, the connection was directly closed after execution. If you want to handle the returned parameters, there is no error. However, I didn't expect the database to operate on the parameters of the asecommand in Sybase ASE ADO. net.

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.