Posts

Showing posts with the label Database

Simple way to generate C# class from database table

 Use the below sql script to generate c# model corresponding to the table.   Set @TableName to the name of your table.   declare @TableName sysname = 'TableName' declare @Result varchar (max) = 'public class ' + @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 'datetim

List all tables to be truncate

--Get the list of all the tables to be truncated   DECLARE @TablesToBeTruncated AS TABLE   (Id INT IDENTITY(1,1),TableObjectId INT , TableName SYSNAME,      SchemaId INT ) INSERT INTO @TablesToBeTruncated   SELECT ST.object_id,ST. name ,ST.schema_id   FROM sys.Tables ST   WHERE ST.type = 'U' AND ST. NAME NOT LIKE '#%'   AND ST. name <> 'sysdiagrams'   --AND ST.NAME NOT IN ('') -- Specify here the comma separated table names for which truncation is not required   --AND ST.NAME IN ('') -- Specify here the comma separated table names which needs to be truncated     --Generate the foreignkeys drop and create back script DECLARE @CreateScript AS NVARCHAR( MAX ), @DropScript AS NVARCHAR( MAX ) SELECT      ------------DROP SCRIPT--------------------      @DropScript = ISNULL (@DropScript, '' ) + 'ALTER TABLE ' + QUOTENAME(SCHEMA_NAME(Tlist.SchemaId)) + '.'       + QUOT

NuGet - Common Database Settings

NuGet Common Database Settings Step 1:  get the nuget package https://www.nuget.org/packages/CommonDatabaseSettings Step 2: Make a object for DataBase class. (you can see many Method ) Call the method named CheckDatabaseConnection . Note : you can get the connection sting by calling DataBase.ConnectionString That's all, select which type of database to be connected. More over there are many methods in the BataBase class. Function Description Return type GetConnectionString(string) Set the database connection string void CheckDatabaseConnection(string) Check the connection string existing or not bool CheckDataExist(string) Check the data is present in the table bool DropdownFill(string) Fill the a Dropdown DataTable ExecuteDB(string) Can insert, update and delete data in the database void GetData(string) Get a strin