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