DataSet ds = new DataSet(); DataTable dt = new DataTable("myTable"); dt.Columns.Add("Id", typeof(int)); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Age", typeof(int)); dt.Rows.Add(1, "John", 25); dt.Rows.Add(2, "Jane", 30); dt.Rows.Add(3, "Jack", 35); ds.Tables.Add(dt);
string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"; string query = "SELECT * FROM myTable"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlDataAdapter adapter = new SqlDataAdapter(query, connection); DataSet ds = new DataSet(); adapter.Fill(ds); DataTable dt = ds.Tables[0]; foreach (DataRow row in dt.Rows) { Console.WriteLine(row["Name"]); } }This code connects to a SQL server and retrieves data from the "myTable" table using a SQL query. It creates a new dataset and populates it with the data returned from the query. It then retrieves the first datatable from the dataset and loops through each row, printing the value in the "Name" column for each row. Package Library: System.Data.SqlClient