WebServices (C #)-return data set query (Dataset)

Source: Internet
Author: User
[Webmethod (description = "function: Return query dataset | parameter: SQL query | return: DataSet | return type: DataSet")]
Public dataset getdataset (string strsql)
{
// Database connection parameters (access to the client application configuration file)
String strconn = @ configurationmanager. connectionstrings ["myconnectionstring"]. connectionstring;
// Create an open connection to the database
// Set to open the database connection
Sqlconnection myconn = new sqlconnection (strconn );
Try // run properly
{
String MySQL = @ strsql. Trim (). tostring ();
// Use the attribute setting specified by connectionstring to open the database connection
Myconn. open ();
// An SQL statement or stored procedure to be executed on the database
Sqlcommand mycomm = new sqlcommand (MySQL, myconn );
// A set of data commands and a database connection, used to fill the dataset and update the data source
Sqldataadapter myadapter = new sqldataadapter ();
// Data cache in memory
Dataset mydataset = new dataset ();
// Query the database
Myadapter. selectcommand = new sqlcommand (strsql, myconn );
// Add or refresh rows in dataset to match rows in the data source
Myadapter. Fill (mydataset );
// Return Dataset
Return mydataset;
}
Catch (sqlexception) // Exception Handling for database operations
{
Console. Write ("<script language = JavaScript> ");
Console. Write ("alert ('system prompt: the current database operation failed or the network is busy. Please try again later! ');");
Console. Write ("</SCRIPT> ");
If (myconn. State = connectionstate. open)
{
// Close the database connection
Myconn. Close ();
}
// Failed to return
Return NULL;
}
Catch // Exception Handling
{
If (myconn. State = connectionstate. open)
{
// Close the database connection
Myconn. Close ();
}
// Failed to return
Return NULL;
}
Finally // clear all resources allocated in the try block after execution
{
If (myconn. State = connectionstate. open)
{
// Close the database connection
Myconn. Close ();
}
}
}

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.