Click here to Skip to main content
12,632,234 members (27,643 online)
Click here to Skip to main content
Add your own
alternative version

Stats

51.9K views
1K downloads
25 bookmarked
Posted

Writing SQL Server Database tables description into Word Document

, 18 Aug 2005
Rate this:
Please Sign up or sign in to vote.
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.

<FONT color=#008000 size=2>

<FONT color=#008000 size=2>//Connection string

<FONT color=#0000ff size=2>string connectionString = "Integrated Security=SSPI;Server={ServerName};Persist Security Info=False;Initial Catalog={DatabaseName};

<FONT color=#008000 size=2>//open the connection

SqlConnection sqlCon = <FONT color=#0000ff size=2>new SqlConnection(connectionString);

sqlCon.Open();

After that construct the SqlCommand ,SqlDataAdpater class objects

<FONT color=#008000 size=2>//set the command text

SqlCommand command = <FONT color=#0000ff size=2>new SqlCommand("SELECT Name, ID FROM sysObjects WHERE sysObjects.Type = 'U'",sqlCon);

command.CommandType = CommandType.Text;

<FONT color=#008000 size=2>//fill the adapter

SqlDataAdapter cadapter = <FONT color=#0000ff size=2>new SqlDataAdapter(command);

DataSet cdataSet = <FONT color=#0000ff size=2>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 = <FONT color=#0000ff size=2>null;

Word._Document doc = <FONT color=#0000ff size=2>null;

doc = word.Documents.Add(<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing, <FONT color=#0000ff size=2>ref missing);

doc.SaveAs(<FONT color=#0000ff size=2>ref file,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing,

<FONT color=#0000ff size=2>                ref missing,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing,

<FONT color=#0000ff size=2>                ref missing,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing);

doc = word.Documents.Open(<FONT color=#0000ff size=2>ref file,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing,

<FONT color=#0000ff size=2>                                       ref missing, <FONT color=#0000ff size=2>ref missing, <FONT color=#0000ff size=2>ref missing, <FONT color=#0000ff size=2>ref missing, <FONT color=#0000ff size=2>ref missing,

<FONT color=#0000ff size=2>                                       ref missing, <FONT color=#0000ff size=2>ref falseValue, <FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>ref missing,<FONT color=#0000ff size=2>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

Share

About the Author

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

You may also be interested in...

Pro

Comments and Discussions

 
GeneralMy vote of 1 Pin
BABABIKHIAL21-Feb-09 22:19
memberBABABIKHIAL21-Feb-09 22:19 
Questionwhy? Pin
weiquan_wang27-Feb-07 3:39
memberweiquan_wang27-Feb-07 3:39 
"word.Documents.Open(...)"
when I compile the project,it is error!
Thank you!


AnswerRe: why? Pin
Lokanatha Reddy23-Mar-07 1:01
memberLokanatha Reddy23-Mar-07 1:01 
GeneralSQL 2005 Get list tables in all databases Pin
itmagistr13-Feb-07 4:49
memberitmagistr13-Feb-07 4:49 
Generalcode nt functional Pin
Nishant__Rai8-Jun-06 3:23
memberNishant__Rai8-Jun-06 3:23 
GeneralRe: code nt functional Pin
jiugarte Brasil27-Feb-07 6:59
memberjiugarte Brasil27-Feb-07 6:59 
GeneralIts a bit of a mess Pin
paulb18-Aug-05 16:28
memberpaulb18-Aug-05 16:28 
GeneralRe: Its a bit of a mess Pin
Danila Korablin18-Aug-05 17:56
memberDanila Korablin18-Aug-05 17:56 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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