Ways to connect to a database with visual Studio and get a dataset

Source: Internet
Author: User

1. First locate your project in Solution Explorer, right-click and choose Add Class.

2. after naming the class, double-click to open it. I'm named SqlHelper here.

Add the following statement in the upper row using the following line:

Using System.Data;
Using System.Data.SqlClient;
Using System.Net;

These are the files that must be called to call the DataSet method and the database connection method.

public class SqlHelper
{


public static DataSet GetDataSet (String sql)
{
String hostinfo = Dns.gethostname (); The connection method used is WINODWS authentication, so get the computer name here.
string s = "Data source=" + hostinfo + "'" + "; initial catalog= ' database '; integrated security= ' true ';     Multipleactiveresultsets= ' true '; The database connection string, where "database" is the name of the database.
SqlConnection conn = new SqlConnection (s); Instantiating a SqlConnection object
DataSet ds = new DataSet (); Instantiating a DataSet
Conn.    Open (); Open a database connection
SqlDataAdapter SDA = new SqlDataAdapter (SQL, conn); SQL is the SQL statement passed over
Sda.  Fill (DS); Populating a DataSet
return DS; return dataset
}

}

3. after writing the class, you can get the dataset directly by calling SqlHelper's GetDataSet method after the SQL statement has been written somewhere else.

After running, the GridView can display the data from the background database.

Ways to connect to a database with visual Studio and get a dataset

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.