What is SQL?
- SQL stands for Structured Query Language
- SQL lets you access and manipulate databases
- SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO) in 1987
What Can SQL do?
- SQL can execute queries against a database
- SQL can retrieve data from a database
- SQL can insert records in a database
- SQL can update records in a database
- SQL can delete records from a database
- SQL can create new databases
- SQL can create new tables in a database
- SQL can create stored procedures in a database
- SQL can create views in a database
- SQL can set permissions on tables, procedures, and views
SQL is a Standard - BUT....
Although SQL is an ANSI/ISO standard, there are different versions of the SQL language.
However, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.
Note: Most of the SQL database programs also have their own proprietary extensions in addition to the SQL standard!
Using SQL in Your Web Site
To build a web site that shows data from a database, you will need:
- An RDBMS database program (i.e. MS Access, SQL Server, MySQL)
- To use a server-side scripting language, like PHP or ASP
- To use SQL to get the data you want
- To use HTML / CSS to style the page
RDBMS
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
The data in RDBMS is stored in database objects called tables. A table is a collection of related data entries and it consists of columns and rows.
Look at the "Customers" table:
Example
SELECT * FROM Customers;SQL Code
Create table and column as you need, like given below.
- CREATE TABLE [dbo].[EmployeeMaster](
- [RowId] [bigint] NULL,
- [EmpFirstName] [varchar](50) NULL,
- [EmpLastName] [varchar](50) NULL,
- [PhoneNo] [bigint] NULL,
- [City] [bigint] NULL,
- [Address] [varchar](500) NULL,
- [DateOfBirth] [datetime] NULL,
- [Gender] [int] NULL,
- [MaritalStatus] [bit] NULL,
- [EmpStatus] [bit] NULL
- ) ON [PRIMARY]
- GO
Usually, you can create class model with the same name as table columns. Often, you look at the table structure and then create property. But now, you just have to pass the table name and types of class.
SQL Code
- CREATE PROCEDURE CREATEMODEL
- (
- @TableName SYSNAME ,
- @CLASSNAME VARCHAR(500)
- )
- AS
- BEGIN
- DECLARE @Result VARCHAR(MAX)
-
- SET @Result = @CLASSNAME + @TableName + '
- {'
-
- SELECT @Result = @Result + '
- public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }'
- FROM
- (
- SELECT
- REPLACE(col.NAME, ' ', '_') ColumnName,
- column_id ColumnId,
- CASE typ.NAME
- WHEN 'bigint' THEN 'long'
- WHEN 'binary' THEN 'byte[]'
- WHEN 'bit' THEN 'bool'
- WHEN 'char' THEN 'string'
- WHEN 'date' THEN 'DateTime'
- WHEN 'datetime' THEN 'DateTime'
- WHEN 'datetime2' then 'DateTime'
- WHEN 'datetimeoffset' THEN 'DateTimeOffset'
- WHEN 'decimal' THEN 'decimal'
- WHEN 'float' THEN 'float'
- WHEN 'image' THEN 'byte[]'
- WHEN 'int' THEN 'int'
- WHEN 'money' THEN 'decimal'
- WHEN 'nchar' THEN 'char'
- WHEN 'ntext' THEN 'string'
- WHEN 'numeric' THEN 'decimal'
- WHEN 'nvarchar' THEN 'string'
- WHEN 'real' THEN 'double'
- WHEN 'smalldatetime' THEN 'DateTime'
- WHEN 'smallint' THEN 'short'
- WHEN 'smallmoney' THEN 'decimal'
- WHEN 'text' THEN 'string'
- WHEN 'time' THEN 'TimeSpan'
- WHEN 'timestamp' THEN 'DateTime'
- WHEN 'tinyint' THEN 'byte'
- WHEN 'uniqueidentifier' THEN 'Guid'
- WHEN 'varbinary' THEN 'byte[]'
- WHEN 'varchar' THEN 'string'
- ELSE 'UNKNOWN_' + typ.NAME
- END ColumnType,
- CASE
- WHEN col.is_nullable = 1 and typ.NAME in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier')
- THEN '?'
- ELSE ''
- END NullableSign
- FROM SYS.COLUMNS col join sys.types typ on col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
- where object_id = object_id(@TableName)
- ) t
- ORDER BY ColumnId
- SET @Result = @Result + '
- }'
-
- print @Result
-
- END
After running this procedure, execute SP with parameters. Now, the result will be loaded as model class.
- exec CREATEMODEL 'EmployeeMaster', 'public class '
Output
- public class EmployeeMaster
- {
- public long? RowId { get; set; }
- public string EmpFirstName { get; set; }
- public string EmpLastName { get; set; }
- public long? PhoneNo { get; set; }
- public long? City { get; set; }
- public string Address { get; set; }
- public DateTime? DateOfBirth { get; set; }
- public int? Gender { get; set; }
- public bool? MaritalStatus { get; set; }
- public bool? EmpStatus { get; set; }
- }
No comments:
Post a Comment