![]() |
Database »
Database »
General
Intermediate
SQL Builder for Microsoft SQL ServerBy Arek SuroboyoThis is a SQL Query Builder which is could be used for further purposes. This was developed under C#.NET. This application will allow us to design a query. It will check any relationship of the tables so there's a little possibility generating query from unrelated tables |
C#, Windows, .NET 2.0VS2005, DBA, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This SQL Builder is developed to build a SQL query statement which will be used for further usage. This tool is still in Beta Version and runs only on Databases which are managed in Microsoft SQL Server.
I have searched and compare many things SQL Builder on the web. But I couldn't find a suitable one. Usually the problem is on the relation between tables included on the project. When we want to create a query, it has to be checked the relationship and it has to be easier so a common user could use this tool without limited SQL syntax knowledge.
There are two main files on this project. The first thing is it's form and the last is a query builder class. There is also a simple documentation of this project. The description below is very simple description. You can directly using this class or reading di documentation file including on zip file.
Let's begin with a query builder class.
There are some important classes. Before we generate a query we have to define our connection string and also generate all table on this database. Here is the code to collect all table.
public DataTable GetListOfDbcTables() { if (this.SQLConnect == null) { this.SQLConnect = this.CreateSqlConnection(); } SqlDataAdapter adapter1 = (SqlDataAdapter)this.CreateDataAdapter("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' " + "AND TABLE_NAME NOT IN ('dtproperties') AND TABLE_NAME NOT IN ('Sysdiagrams') ORDER BY TABLE_TYPE", this.SQLConnect); DataTable table1 = new DataTable(); adapter1.Fill(table1); return table1; } public DataTable GetListOfTableFields(string tableName) { this.CurrentTableName = tableName; this.GetTableSchema = "SELECT COLUMN_NAME,DATA_TYPE,isnull(CHARACTER_MAXIMUM_LENGTH,0) AS 'LENGTH',IS_NULLABLE, " + "ISNULL((SELECT 'Y' FROM SYSFOREIGNKEYS WHERE FKEYID =ID AND FKEY=COLID),'N') as 'IsForeignKey'," + "Ordinal_Position, SYSCOLUMNS.IsComputed, IsIdentity, IsRowGuidCol " + "FROM SYSCOLUMNS, " + "(SELECT COLUMN_NAME, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, Ordinal_Position, " + "COLUMNPROPERTY(OBJECT_ID('" + tableName + "'), INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME, 'IsIdentity') AS IsIdentity, " + "COLUMNPROPERTY(OBJECT_ID('" + tableName + "'), INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME, 'IsRowGuidCol') AS IsRowGuidCol " + "FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='" + tableName + "') AS A " + "WHERE ID IN (SELECT ID FROM SYSOBJECTS WHERE TYPE='U' AND NAME ='" + tableName + "') AND A.COLUMN_NAME =NAME " + "Order By Ordinal_Position"; if (this.SQLConnect == null) { this.SQLConnect = this.CreateSqlConnection(); } SqlDataAdapter adapter1 = (SqlDataAdapter)this.CreateDataAdapter(this.GetTableSchema, this.SQLConnect); DataTable table1 = new DataTable(); adapter1.Fill(table1); table1.DefaultView.Sort = "COLUMN_NAME ASC"; return table1; } /// <summary> /// Get All Parent from each of Foreign Key /// </summary> /// <param name="TableName"></param> /// <returns></returns> public DataTable GetParentForeignKey(string TableName) { this.currentTableName = TableName; this.DefineForeignKey = "Select b.Name As TableName, c.column_name As ForeignKeyField, " + "(Select Name from SysObjects Where ID = a.rkeyid) As ForeignTable," + "(Select Column_Name from INFORMATION_SCHEMA.COLUMNS Where Table_Name IN " + "(Select Name from SysObjects Where ID = a.rkeyid) " + "AND Ordinal_Position = a.rkey) As ForeignField " + "FROM Sysforeignkeys a, sysobjects b, information_schema.columns c " + "Where a.fkeyid = b.id AND b.name ='" + this.currentTableName + "' AND a.fkey = c.ordinal_position AND " + "c.Table_Name = '" + this.currentTableName + "'"; if (this.SQLConnect == null) { this.SQLConnect = this.CreateSqlConnection(); } SqlDataAdapter adapter1 = (SqlDataAdapter)this.CreateDataAdapter(this.DefineForeignKey, this.SQLConnect); DataTable table1 = new DataTable(); adapter1.Fill(table1); return table1; } public DataTable GetHostedForeignKey(string TableName) { this.currentTableName = TableName; this.DefineForeignKey = "Select b.Name As TableName, c.column_name As ForeignKeyField, " + "(Select Name from SysObjects Where ID = a.fkeyid) As ForeignTable," + "(Select Column_Name from INFORMATION_SCHEMA.COLUMNS Where Table_Name IN " + "(Select Name from SysObjects Where ID = a.fkeyid) " + "AND Ordinal_Position = a.fkey) As ForeignField " + "FROM Sysforeignkeys a, sysobjects b, information_schema.columns c " + "Where a.rkeyid = b.id AND b.name ='" + this.currentTableName + "' AND a.rkey = c.ordinal_position AND " + "c.Table_Name = '" + this.currentTableName + "'"; if (this.SQLConnect == null) { this.SQLConnect = this.CreateSqlConnection(); } SqlDataAdapter adapter1 = (SqlDataAdapter)this.CreateDataAdapter(this.DefineForeignKey, this.SQLConnect); DataTable table1 = new DataTable(); adapter1.Fill(table1); return table1; } /// <summary> /// Get Parent from a foreign key /// </summary> /// <param name="TableName"></param> /// <param name="FKFieldName"></param> /// <returns></returns> public DataTable GetParentForeignKey(string TableName, string FKFieldName) { this.currentTableName = TableName; this.DefineForeignKey = "Select b.Name As TableName, c.column_name As ForeignKeyField, " + "(Select Name from SysObjects Where ID = a.rkeyid) As ForeignTable," + "(Select Column_Name from INFORMATION_SCHEMA.COLUMNS Where Table_Name IN " + "(Select Name from SysObjects Where ID = a.rkeyid) " + "AND Ordinal_Position = a.rkey) As ForeignField " + "FROM Sysforeignkeys a, sysobjects b, information_schema.columns c " + "Where a.fkeyid = b.id AND b.name ='" + this.currentTableName + "' AND a.fkey = c.ordinal_position AND " + "c.Table_Name = '" + this.currentTableName + "' AND c.Column_Name = '" + FKFieldName + "'"; if (this.SQLConnect == null) { this.SQLConnect = this.CreateSqlConnection(); } SqlDataAdapter adapter1 = (SqlDataAdapter)this.CreateDataAdapter(this.DefineForeignKey, this.SQLConnect); DataTable table1 = new DataTable(); adapter1.Fill(table1); return table1; } /// <summary> /// Get foreign key relation from two tables /// </summary> /// <param name="TableName1"></param> /// <param name="TableName2"></param> /// <returns></returns> public DataTable GetForeignKeyRelation(string TableName1, string TableName2) { this.currentTableName = TableName1; this.DefineForeignKey = "Select b.Name As TableName, c.column_name As ForeignKeyField, " + "(Select Name from SysObjects Where ID = a.rkeyid) As ForeignTable, " + "(Select Column_Name from INFORMATION_SCHEMA.COLUMNS Where Table_Name IN " + "(Select Name from SysObjects Where ID = a.rkeyid) " + "AND Ordinal_Position = a.rkey) As ForeignField " + "FROM Sysforeignkeys a, sysobjects b, information_schema.columns c " + "Where a.fkeyid = b.id AND b.name ='"+TableName1+"' AND a.fkey = c.ordinal_position AND " + "c.Table_Name = '" + TableName1 + "' AND a.rkeyid IN (Select ID from sysobjects where name = '" + TableName2 + "') "; if (this.SQLConnect == null) { this.SQLConnect = this.CreateSqlConnection(); } SqlDataAdapter adapter1 = (SqlDataAdapter)this.CreateDataAdapter(this.DefineForeignKey, this.SQLConnect); DataTable table1 = new DataTable(); adapter1.Fill(table1); return table1; }
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 24 Apr 2007 Editor: |
Copyright 2007 by Arek Suroboyo Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |