Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 585
  • Last Modified:

How to create SQL Server 2005 Database, Tables and Fields

Hello,

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.

Regards,
Stef
0
Stef Merlijn
Asked:
Stef Merlijn
2 Solutions
 
SteveBayCommented:
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.
0
 
JohnjcesCommented:
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.

John

PS.

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';
 ADOCommand1.Execute;

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

Open in new window

0
 
JohnjcesCommented:
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.

John

0
 
Geert GruwezOracle dbaCommented:
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
http://msdn.microsoft.com/en-us/library/ms189826(SQL.90).aspx

and for creating databases :
http://msdn.microsoft.com/en-us/library/ms176061(SQL.90).aspx

creating a table:
http://msdn.microsoft.com/en-us/library/ms174979(SQL.90).aspx

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 :)

0
 
Stef MerlijnDeveloperAuthor Commented:
Thank you all.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now