C# Sql Select Tutorial MS SQL Server

In this C# tutorial, we will select users table from MS SQL Server using C# code sample below.

C# SQL Select Code

using System.Data.SqlClient;
using System.Data;

public DataSet GetUsersTable()
{
 string constr = 
 "Provider=sqloledb;Data Source=SqlServerIPAddress;Initial " +
 "Catalog=DataBaseName;User Id=UserName;Password=UserPassword;";
  
 string str_select = "SELECT * FROM tbl_users";

 SqlConnection con = new SqlConnection(constr);
 DataSet ds = new DataSet();
 SqlDataAdapter da = new SqlDataAdapter(str_select, con);

 da.Fill(ds);
 return ds;
 }

Note that you can also use this code sample in ASP.NET.