Database

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 SELECTUPDATEDELETEINSERTWHERE) 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;

Convert Table Column Into C# Model Class

SQL Code

Create table and column as you need, like given below.

  1. CREATE TABLE [dbo].[EmployeeMaster](  
  2.     [RowId] [bigint] NULL,  
  3.     [EmpFirstName] [varchar](50) NULL,  
  4.     [EmpLastName] [varchar](50) NULL,  
  5.     [PhoneNo] [bigint] NULL,  
  6.     [City] [bigint] NULL,  
  7.     [Address] [varchar](500) NULL,  
  8.     [DateOfBirth] [datetime] NULL,  
  9.     [Gender] [int] NULL,  
  10.     [MaritalStatus] [bit] NULL,  
  11.     [EmpStatus] [bit] NULL  
  12. ) ON [PRIMARY]  
  13. 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

  1. CREATE PROCEDURE CREATEMODEL  
  2. (  
  3.      @TableName SYSNAME ,  
  4.      @CLASSNAME VARCHAR(500)   
  5. )  
  6. AS  
  7. BEGIN  
  8.     DECLARE @Result VARCHAR(MAX)  
  9.   
  10.     SET @Result = @CLASSNAME + @TableName + '  
  11. {'  
  12.   
  13. SELECT @Result = @Result + '  
  14.     public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }'  
  15. FROM  
  16. (  
  17.     SELECT   
  18.         REPLACE(col.NAME, ' ''_') ColumnName,  
  19.         column_id ColumnId,  
  20.         CASE typ.NAME   
  21.             WHEN 'bigint' THEN 'long'  
  22.             WHEN 'binary' THEN 'byte[]'  
  23.             WHEN 'bit' THEN 'bool'  
  24.             WHEN 'char' THEN 'string'  
  25.             WHEN 'date' THEN 'DateTime'  
  26.             WHEN 'datetime' THEN 'DateTime'  
  27.             WHEN 'datetime2' then 'DateTime'  
  28.             WHEN 'datetimeoffset' THEN 'DateTimeOffset'  
  29.             WHEN 'decimal' THEN 'decimal'  
  30.             WHEN 'float' THEN 'float'  
  31.             WHEN 'image' THEN 'byte[]'  
  32.             WHEN 'int' THEN 'int'  
  33.             WHEN 'money' THEN 'decimal'  
  34.             WHEN 'nchar' THEN 'char'  
  35.             WHEN 'ntext' THEN 'string'  
  36.             WHEN 'numeric' THEN 'decimal'  
  37.             WHEN 'nvarchar' THEN 'string'  
  38.             WHEN 'real' THEN 'double'  
  39.             WHEN 'smalldatetime' THEN 'DateTime'  
  40.             WHEN 'smallint' THEN 'short'  
  41.             WHEN 'smallmoney' THEN 'decimal'  
  42.             WHEN 'text' THEN 'string'  
  43.             WHEN 'time' THEN 'TimeSpan'  
  44.             WHEN 'timestamp' THEN 'DateTime'  
  45.             WHEN 'tinyint' THEN 'byte'  
  46.             WHEN 'uniqueidentifier' THEN 'Guid'  
  47.             WHEN 'varbinary' THEN 'byte[]'  
  48.             WHEN 'varchar' THEN 'string'  
  49.             ELSE 'UNKNOWN_' + typ.NAME  
  50.         END ColumnType,  
  51.         CASE   
  52.             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')   
  53.             THEN '?'   
  54.             ELSE ''   
  55.         END NullableSign  
  56.     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  
  57.     where object_id = object_id(@TableName)  
  58. ) t  
  59. ORDER BY ColumnId  
  60. SET @Result = @Result  + '  
  61. }'  
  62.   
  63. print @Result  
  64.   
  65. END  

After running this procedure, execute SP with parameters. Now, the result will be loaded as model class.

  1. exec CREATEMODEL 'EmployeeMaster''public class '  

Output

  1. public class EmployeeMaster  
  2. {  
  3. public long? RowId { getset; }  
  4. public string EmpFirstName { getset; }  
  5. public string EmpLastName { getset; }  
  6. public long? PhoneNo { getset; }  
  7. public long? City { getset; }  
  8. public string Address { getset; }  
  9. public DateTime? DateOfBirth { getset; }  
  10. public int? Gender { getset; }  
  11. public bool? MaritalStatus { getset; }  
  12. public bool? EmpStatus { getset; }  
  13. }  

No comments:

Post a Comment