How to create SQL Server 2005 Database, Tables and Fields

Posted on 2008-11-03
Last Modified: 2010-04-21

Can anybody give me some sample on how to create the following by code in Delphi:
- add SQL Server 2005 Database
- add a table
- add different types of fields to the table

I'm using Delphi 2007 Enterprise. Connection to SQL Server is currently done via ADOConnection.
I also heared that there is a possibility to use scripts to populate a database.

Question by:Delphiwizard
    LVL 14

    Expert Comment

    An easiest way to do this is by using SQL statements that you can generate from the Microsoft SQL Server Management Studio.
    Design the database (and tables) in Mgmt Studio then right click on the DB (or table) and choose 'Script Database as'&> 'Create To'&> 'Clipboard'. Then paste that SQL code into your Program and assign the text to the SQL property of a TQuery component. You can do the same with a Table.
    LVL 18

    Accepted Solution

    Here's a code snippet I use to add or create a table and add fields. The fields I added in this example are Recd which is a DateTime field and a primary key, Who which is Text at 20 characters and CD Text which is 50 characters.

    Again in the exmaple you must be already connected to the SQl Server and to the database.



    Oh, and to create an empty database you need an ADO connection to a server and generally the command is:

     ADOCommand1.CommandText := 'Create DataBase MyDatabase';

    procedure TFCDLibDBUpdate.Button1Click(Sender: TObject);
     ADOCommand1.CommandText := 'Create Table CheckOut (Recd DateTime Primary Key, Who Text(20), CD Text(50))';
       on E:Exception do
     ShowMessage('Your database was successfully updated.');

    Open in new window

    LVL 18

    Expert Comment

    If I were you i would create the database using MS Enterprise Manager or other free tool and ship that with my code and then attach the database using the code I provided earlier. I would also have the empty database fully populated with all of fields, my database schema, and ready to go. I would only add tables should my application require it.

    Just my preference... a lot of guys do create DBs and all that stuff on the fly form their application.


    LVL 36

    Assisted Solution

    by:Geert Gruwez
    SQL Server has an online help system in which these methods are described with the Enterprise manager and with Transact SQL
    if you search for Transact SQL Reference you will find the msdn site at the top of the list

    and for creating databases :

    creating a table:

    Transact-SQL is the language used by MSSQL Database
    It takes a bit of reading to get used to the language, but once you master this, the world of MSSQL databases lays at your feet :)


    Author Closing Comment

    Thank you all.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
    INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now