Click here to Skip to main content
Click here to Skip to main content

Writing SQL Server Database tables description into Word Document

By , 18 Aug 2005
 

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 connection

SqlConnection sqlCon = new SqlConnection(connectionString);

sqlCon.Open();

After that construct the SqlCommand ,SqlDataAdpater class objects

//set the command text

SqlCommand command = new SqlCommand("SELECT Name, ID FROM sysObjects WHERE sysObjects.Type = 'U'",sqlCon);

command.CommandType = CommandType.Text;

//fill the adapter

SqlDataAdapter 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();

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

satya.rapelly
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalfree database documentation toolmemberNSPatel3827 Apr '11 - 18:18 
Why someone would spent time when free sql database documentation tool[^] alredy out there
GeneralMy vote of 1memberBABABIKHIAL21 Feb '09 - 21:19 
code not working without many changes, poor documentation
Questionwhy?memberweiquan_wang27 Feb '07 - 2:39 
"word.Documents.Open(...)"
when I compile the project,it is error!
Thank you!
 

AnswerRe: why?memberLokanatha Reddy23 Mar '07 - 0:01 
try with...
 
Object strPathName = "D:\\tests\\Tables1.doc";
object oMissing = System.Reflection.Missing.Value;
oDoc1 = oWord.Documents.Open(ref strPathName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
 
If its not working, check the msoffice version and compatibility...
 
Loka
GeneralSQL 2005 Get list tables in all databasesmemberitmagistr13 Feb '07 - 3:49 
How make it for All Databases in MS SQL 2005?

Generalcode nt functionalmemberNishant__Rai8 Jun '06 - 2:23 
hi
plz check it !!! i downoaded the code and when i tried to run it it says Object ref not set to instance of an object
at word.visible=true;
 
why is that problem there!!
GeneralRe: code nt functionalmemberjiugarte Brasil27 Feb '07 - 5:59 
Change:
 
private void btnGenerate_Click(object sender, System.EventArgs e)
{
Word._Application word = null;
Word._Document doc = null;
 
to:
 
private void btnGenerate_Click(object sender, System.EventArgs e)
{
Word._Application word = new Word.ApplicationClass();
Word._Document doc = null;
 
and all is ok.Smile | :)

 
jiugarte brasil

GeneralIts a bit of a messmemberpaulb18 Aug '05 - 15:28 
You could have left out all the database stuff, there are dozens of articles on ADO.NET already. Would have been better to explain more about the Office automation side of things.
 
Also the download links don't work.
GeneralRe: Its a bit of a messmemberDanila Korablin18 Aug '05 - 16:56 
Smile | :) Yeah, they are here:
http://www.codeproject.com/useritems/DB_Documentor/DB_Documentor_demo.zip[^]
 
http://www.codeproject.com/useritems/DB_Documentor/DB_Documentor_src.zip[^]
 
Wormhole is the God divided by zero

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 18 Aug 2005
Article Copyright 2005 by satya.rapelly
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid