Writing SQL Server Database tables description into Word Document





1.00/5 (8 votes)
Aug 18, 2005
2 min read

59641

1105
Window application generates the document with Sql Server database tables
Introduction
This tool a simple Windows application which take path of word document where would like you to create and Microsoft Sql Server Database name.
This application makes use of Word Application Library "Microsoft Office 10.0 Object Library" and "Microsoft Word 10.0 Object Library" to create the document.
How it Works:
First it will try to open the database, it uses SqlConnection class of System.Data namespace,then SqlCommand,SqlDataAdapter classes to get the data from SqlServer database.
//Connection string string connectionString = "Integrated Security=SSPI;Server={ServerName};Persist Security Info=False;Initial Catalog={DatabaseName}; //open the connectionSqlConnection sqlCon =
new SqlConnection(connectionString);sqlCon.Open();
After that construct the SqlCommand ,SqlDataAdpater class objects
//set the command textSqlCommand command =
new SqlCommand("SELECT Name, ID FROM sysObjects WHERE sysObjects.Type = 'U'",sqlCon);command.CommandType = CommandType.Text;
//fill the adapterSqlDataAdapter cadapter =
new SqlDataAdapter(command);DataSet cdataSet =
new DataSet();cadapter.Fill(cdataSet);
To get the database tables information from Database we use - following query:
"SELECT Name, ID FROM sysObjects WHERE sysObjects.Type = 'U'"
After fetching the database tables from DB, to get the columns descriptions associated with DB Table - uses following Query:
SELECT sysColumns.Name AS ColumnName," +
"sysTypes.name AS DataType, "+
"sysColumns.Length AS Length, " +
"sysColumns.Status AS Status " +
"FROM sysColumns, sysTypes " +
"WHERE sysColumns.id = " + {table_name}+
" AND sysColumns.usertype = sysTypes.usertype";
Costruct the column information and Make Word Table format and write into the document.
And close the database connection
How to use the Word,Document classes:
Add the following references -
---- Microsoft Office 10.0 Object Library
---- Microsoft Word 10.0 Object Library
Word._Application word =
null;Word._Document doc =
null;doc = word.Documents.Add(
ref missing,ref missing,ref missing, ref missing);doc.SaveAs(
ref file,ref missing,ref missing,ref missing,ref missing,ref missing, ref missing,ref missing,ref missing,ref missing,ref missing,ref missing, ref missing,ref missing,ref missing,ref missing);doc = word.Documents.Open(
ref file,ref missing,ref missing,ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref falseValue, ref missing,ref missing,ref missing);doc.Activate();