C#: Easy SQL Server Tool
You can download the Source Code from this link Download Source Code
Introduction
* Kindly view Youtube Video Link to learn more in detail Easy SQL SERVER Tool Using C#.
https://i1.code.msdn.s-msft.com/easy-sql-server-tool-using-6b0b7795/image/file/146958/1/utube.png
Easy SQL Server Tool will allow users to Create SQL Server Database, Delete Database, Load all Database Names from a SQL selected Server, Create a New Table, Insert records to Table, Select Records from Table and bind the result to grid, Export the selected result to CSV file format, Delete Table, Delete ALL Records from a Table without writing any SQL Script.
We have used SQL Server Management Objects (SMO) to programmatically create SQL Server Database, Table etc.
What is SQL Server Management Objects (SMO)?
SQL Server Management Objects (SMO) is a collection of Objects to manage SQL Server programmatically. Using SMO objects we can programmatically connect to SQL server, Get collections of all Database names ,Create new Database, Backup Database, Restore Database, Create Table, Get collections of Table Name for a given Database ,Get all Column details of a given Table and etc.
Reference website: To learn more detail about SQL Server SMO kindly view all this link
Features in Easy SQL Server Tool
https://code.msdn.microsoft.com/site/view/file/146769/1/1.PNG
- Connect to SQL Server.
- Table Details.
- SQL Script Tab (Create Table tab / Insert and Delete records tab / Select Query tab)
- Output Window.
Here we will see details of each part
Procedure
1. Connect to SQL Server
https://code.msdn.microsoft.com/site/view/file/146770/1/1.gif
SQL Server Connect
We can connect to any SQL Server with in our Network. We need to give SQL Server Name or IP Address, SQL Server Login ID and password to connect to SQL Server.
Create New Database
Check for User entered Database name is exist in SQL Server. If exist display the message to user .if there is no database with user entered name then new Database will be created.
https://code.msdn.microsoft.com/site/view/file/146772/1/2.gif
Load Database
Load all the Database Name to combobox for creating Table.
Delete Database
User can delete the selected Database from SQL Server. Before deleting confirmation box will be displayed to user to delete or not. If user click on ok then the selected database will be deleted.
https://code.msdn.microsoft.com/site/view/file/146773/1/3.gif
Database Backup
Backup the selected Database to any folder as you have selected.
https://code.msdn.microsoft.com/site/view/file/146774/1/2.PNG
Database Restore
User can restore the database by selecting ".bak" file from the selected folder.
https://code.msdn.microsoft.com/site/view/file/146775/1/3-1.gif
2. Table Details
https://code.msdn.microsoft.com/site/view/file/146776/1/4.gif
Load Table
User need to first select the Database to list all the Table Names. After selecting the database and by Clicking on the Load Table Names will load all the Table Names to the Combobox .
Delete Table
User can delete the selected Table from SQL Server. Before deleting confirmation box will be displayed to user to delete or not. If user click on ok then the selected Table will be deleted.
3. SQL Script Tab
Here we will be having 3 tab, first tab is for Creating Table, second tab is for Insert and Delete Records and the last third tab is for select Query We will see here all three tab details here.
Create Table Tab
https://code.msdn.microsoft.com/site/view/file/146778/1/5.gif
Here user can create their own table without writing SQL Query. First user need to select the Database where the table need to be created. After Selecting the Database user can enter their SQL Table Name in the textbox. After entering the table name user need to add columns for that Table. Without adding Columns the Table cannot be created.
Adding Columns
https://code.msdn.microsoft.com/site/view/file/146779/1/6.gif
User can add columns for creating new Table. All this columns will be added locally to the grid for finally creating a Table. User can add any number of columns, Delete the Selected Column and also delete all columns by clicking on Clear Columns .Adding and deleting columns is not deals with database so this will be as temp place to add columns for creating Table. After adding all columns for the Table user can click on Create Table Button to create a new Table with given Column details.
Datatype
Here for now only 3 Datatype has been used.
- Int (Numbers)
- varchar(for text)
- nVarchar(for Numbers and text)
User can also add each column data size .For integer the size will not be used, but for Varchar and nVarchar Datatype user can add the column data size .User can also set Max size for both Varchar and nVarchar type by selecting the Max Size checkbox.
For example we can see the below image. Here we have added 4 columns to the grid and adding one more new column and we can see each column Datatype and Size from the grid. After adding all column, we click the Create Table Button .We can see the confirmation message as Table was created and also we can see our new Table has been added in the Table List Combobox.
https://code.msdn.microsoft.com/site/view/file/146780/1/7.gif
Insert and Delete Records Tab
We will be using the recently created test Table for inserting demo.
https://code.msdn.microsoft.com/site/view/file/146781/1/8.gif
Here we can see in the above image as we have selected the Table "test" from Table List. To insert record to selected Table we need to click on the load table Details to insert button. By clicking this we will be displaying all the column name with textbox. Depend on the Datatype of each column we will be adding textbox with maxlength. If the column datatype is Integer then we will be adding Numeric textbox for that column by this user can enter only numbers to that textbox. For Varchar and nVarchar we will check for each Column Size if its max then we set the textbox maxlength as 200 by this user can enter only 200 characters. If size is set for column then we will be setting the column size for textbox maxlength.
Insert
https://code.msdn.microsoft.com/site/view/file/146784/1/9.gif
After loading all the column details with TextBox user can enter the value to be insert for each column. Once user entered the value click on the **Insert into Selected Table **button for inserting a new record. Same like this user can enter any number of records for the selected table. If you want to add more functionality from the code you can add your functionality for example like checking duplicate value before insert and etc.
Delete All Records
User can delete all records of the selected table. Here in this sample we are not checking any condition for deleting the records. If you need to add more functionality you can change from the code and add your own functions for example like Delete records of a table based on condition and etc.
Select Query Tab
https://code.msdn.microsoft.com/site/view/file/146785/1/10.gif
For selecting the records from a Database and display the result .User need to first select the Database and then select the Table from the Table List to create a SQL Select query.
User can select the details using three features
- **All Columns: **If All Columns checkbox is checked then all column details result will be display to the output window grid. (This is same like * in select query for example select * from tablename).We can see from the above image The All Column Check box will be loaded by default .After selecting the table and when user clicks on Run Select Query. All Column details will be loaded in the below output window grid.
- **User selected Columns: **If user wants to display only particular columns of the table then they can click on **load Table Columns to Select **button to display all the columns name of the tables to CheckedListbox. Here user can select there need columns .by default we will be checked all the Column Names. User can uncheck not needed columns and also uncheck the **All Columns **Checkbox to display only selected columns as a result.
- User can Write SQL Select Query: User can also write their SQL Select query to display the output.
https://code.msdn.microsoft.com/site/view/file/146788/1/11.gif
For example if user wants to write their own SQL query to join 3 table and display the result. To write SQL Select query user need to check the **SQL Select Query **checkbox and after writing select query user can click on Run Select Query button to see the result. In the above image we can see, we have write sample SQL Join query to join three table and by clicking the Run Select Query the result has been bind in the grid.
SQL Injection Checking in user entered select query:
https://code.msdn.microsoft.com/site/view/file/146789/1/12.gif
We have also checking for the SQL Injection before executing user entered SQL Select query. We have created an array list to add all SQL injection string and we will be checking is any of the array word is matching with user entered select query. For example we can see the above image as after select query we have enter drop query. But when we click on Run Select Query button we display the message as drop is not accepted in select query.
Here is the list of SQL Injection string I am checking if you need you can add or remove as per your requirement.
|
Save Select Query User can also save the Select Query as text file to selected folder for use the select query later.
https://code.msdn.microsoft.com/site/view/file/146790/1/13.gif
Open saved Select Query:
User can also open the saved Select Query to execute the select script.
https://code.msdn.microsoft.com/site/view/file/146791/1/14.gif
Export the Result to CSV file Format:
User can also export the select result to CSV format. Here in below image we can see the result has been exported as CSV file.
https://code.msdn.microsoft.com/site/view/file/146792/1/15.gif
Building the Sample
Prerequisites
Visual Studio 2015
You can download it from here.
SQL SERVER SMO Reference
For working with SQL Server SMO we need add 4 important reference to our project
Microsoft.SqlServer.ConnectionInfo.dll
Microsoft.SqlServer.Smo.dll
Microsoft.SqlServer.Management.Sdk.Sfc.dll
Microsoft.SqlServer.SqlEnum.dll
You can find this reference from this path (Here we have used SQL Server 2014)
C:\Program Files\Microsoft SQL Server\120\SDK\Assemblies\ . Add all this 4 Assembly reference to your project
https://code.msdn.microsoft.com/site/view/file/146793/1/2.PNG
Description
The main aim was to create a simple and easy to use Easy SQL Tool. All the functionality has been added in one main form with Tab. We have created 2 Class
- smoSQLServerClass in this class we have created function to Connect, Disconnect to SQL Server, Create Database, Delete Database, Backup Database, Write all the Exception to Log text file to executable folder, Create Table etc.
- sqlBizClass: In this Class we perform all business logic for our Main form for example like creating, Dynamic query, Creating Dynamic Controls and bind to panel, Bind all Table names returned from SMOSQLServerClass to Combobox controls ,Check SQL injection for Select query and return the result as true or false etc.
Here we will see some of important code part used in this Easy SQL Server Tool. All the code part has been well-commented, so the user can easily understand the code.
Connect to SQL Server
In this article I have mentioned as for using SQL Server SMO objects we need to add 4 important Assembly reference to our project. Using SMO ServerConnection we can connect to SQL server. We need to set the SQL Server name to ServerInstance and set SQL Server login and password. After connecting we check for is Connection is open and return the Boolean value to our main function to display the appropriate message to user.
|
Write Exception Message to Log File
We will be writing all exception error message to Log file with Date and Time.
|
Load Database Name
In Load Database Name button click event ,we will pass the comboBox control to the Biz class method .In biz class method we will call the SMO Class to get all the Database name and add all the database name to combobox. Here in this method I will check for Master Database and will not add the name for security reason for example user can delete the database from our main form. Similarly you can change the code to restrict any database that show it in our main form.
|
In SMO Class we will be loading all the Database name for a given SQL Server and return as DatabaseCollection to calling class.
|
Create Database
In this function first we check for Database already exist with the user entered name. If database with same name not exist we will create a new database in our SQL Server. If Database already exist in the SQL Server return the message as Database exist.
|
Create Table:
In Create button click we will pass the user selected Database Name, User entered Table Name with Column Details as DataTable to SMO Class. In this function we will check for Table already exist, if exist return false message to user and if Table not already exist then we will create a new Table with column details.
|
Load Column Details for Insert:
In Load Column details button click we will pass the Panel Control to our biz class to get all the column details of selected table and bind a text box with column name to panel .from our SMO Class we will get all the Column details of table and return as **ColumnCollection. **In biz class using foreach we will add all column details like Name as Label control to display the Column Name and add a Textbox for user input .In this method we will check for column type and Column size .If the column type is Integer then we will set the Textbox as Numeric Textbox. If the Column type is Varchar or NVarchar we check for Column length and assign the length as TextBox Maxlenght.
|
In SMO Class loadTableColumnDetails Method we will get all the Column details for given database and table name return the column details as ColumnCollection top biz class for adding dynamic controls for user input for inserting records.
|
Insert Records
In Insert button click we pass the user added insert details panel Control with Database Name and Table Name to biz Class. In biz class we will create a dynamic Insert query with adding all the column names and add all the insert values as parameter and pass the SqlCommand to SMO Class insertQuery method for insert new record to the given Table.
|
Select Query
In Select button click we check for select query type if SQL Select Query check box is not checked then we will pass all the column details to biz class selectRecordsfromTableQuery Method to create a dynamic SQL Select query .If user selected all Columns then we will be using "select * from tablename".If user checked only few columns to display then we will create a dynamic query and adding all user selected columns and return the result as DataTable to bind in grid.
|
Downloads
You can download the Source Code from this link Download Source Code