Posts

Showing posts with the label SQL

Load data in datatable from SQL Server database

Loading data from database is very basic part of every application which uses database as data store. Let's see how we can load data in DataTable from database using ADO.NET from a SQL server database. DataTable dt = new DataTable (); using ( SqlConnection con = new SqlConnection ( ConfigurationManager .ConnectionStrings[ "MyConnectionString" ].ConnectionString)) {    using ( SqlCommand cmd = new SqlCommand ())    {       cmd.CommandText = "Select * from tblUsers" ;       cmd.Connection = con;       cmd.CommandType = CommandType .Text;       using ( SqlDataAdapter adapter = new SqlDataAdapter (cmd))       {          adapter.Fill(dt);         }     }  }

Temporary Tables

Temporary Tables Introduction Temporary Tables are a great T-SQL feature that lets you store and process intermediate results by using the same selection, update, and join capabilities that you can use with typical SQL Server tables. Types of Temporary Tables in SQL You can create two types of temporary tables in SQL, local and global temporary tables. The two types of temporary tables, local and global, differ from each other in their names, their visibility, and their availability. Local Temporary Tables Local temporary tables prefix with single number sign (#) as the first character of their names, like (#table_name). Local temporary tables are visible only in the current session OR you can say that they are visible only to the current connection for the user. They are deleted when the user disconnects from instances of Microsoft SQL Server.   Global Temporary Tables Global temporary tables prefix with double number sign (##) as the first character of their names, like (##table_...

SQL Alter Statement

Alter Statement: Alter statement is used to modify table and its contents. Alter statement is used for making changes in tables. You can change column data type, add column, delete column and also you can add and remove constraints by using alter statement. Let’s see in how many different ways alter statement can be used. General Syntax for alter statement: alter table table_name add,drop column,constraint Suppose we have a table Doctor with following columns and their data types. Column Name Data Type doctor_id varchar(20) name varchar(20) Fee int timings char(10) Assume we have some data in it as follow. doctor_id name Fee timings D101 John 10000 8-4        D102 Peter 20000 4-8        D103 Nash 30000 12-8       D104 Andrew 25000 6-12       D105 Smith 15000 6-12       Adding a column Suppose that we want to add a column in the table we m...