0

Today i'm working on a project where I will create a relational database through source code and not through the built-in wizard.I have been looking for tutorials which explain to me the processes of doing this but seem to not be able to do so. Most have tutorials on how to use the build-in wizard and add content to tables, my main goal is to actually have a utility that users could use which includes a self-building database. if you have examples of this, I would greatly appreciate it or if you know of any good tutorials that will be helpful too

Thanks!

 class Program
{

  static  string strcon = @"user id  = sde ; password = passrd;
    server =dfgserver;database =valrollclients";


   static SqlCommand cmdinserted = new SqlCommand();
   static SqlConnection con; //declaring a connection object 

    static void Main(string[] args)
    {


        cmdinserted.CommandText = "[dbo].[prcinsert_client]";


        cmdinserted.CommandTimeout = 0;


        cmdinserted.CommandType = CommandType.StoredProcedure; 



        cmdinserted.Connection = con; 
        cmdinserted.Parameters.Add("@client_name",
            SqlDbType.VarChar, 12).Value = "me";

        cmdinserted.Parameters.Add("@client_lastname",
           SqlDbType.VarChar, 15).Value = "abutair";

        cmdinserted.Parameters.Add("@client_age ",
           SqlDbType.Int, 4).Value = 4;

        try
        {
            con.Open(); //open connection

            cmdinserted.ExecuteNonQuery(); //execute the stored procedure

            con.Close();//close connection
        }
        catch (SqlException) //catch an error
        {
            throw; //throw it back to the calling method 
        }
haysam
  • 401
  • 5
  • 11
  • 16
  • Entity Framework Code First? – Lotok Jul 16 '13 at 14:42
  • Your question is not clear. – SOfanatic Jul 16 '13 at 14:42
  • Good point to start would be determine what database you mean ? Relation ? File ? or other. – Damian Leszczyński - Vash Jul 16 '13 at 14:43
  • erm, what database engine, SQL Server? There this thing called TSQL which you can use to script database tasks. Lots of it is standard SQL across many database engines http://msdn.microsoft.com/en-us/library/ms176061.aspx – Jodrell Jul 16 '13 at 14:44
  • my goal is to create a database with tables that will be created curing build-time but I usually use a wizard for this but this time I want to try creating the db manually – haysam Jul 16 '13 at 14:47
  • here is a link - http://support.microsoft.com/kb/307283 and here are some links on SO that indicate this might be considered a duplicate - http://stackoverflow.com/questions/9015142/creating-a-database-programmatically-in-sql-server - and - http://stackoverflow.com/questions/7363508/how-to-create-a-sql-server-database-programmatically-in-c-sharp – BillH Jul 16 '13 at 15:02

2 Answers2

0

This is the code you have to run on the server:

USE master;
GO
CREATE DATABASE Sales
ON 
( NAME = Sales_dat,
    FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL        \DATA\saledat.mdf',
    SIZE = 10,
    MAXSIZE = 50,
    FILEGROWTH = 5 )
LOG ON
( NAME = Sales_log,
    FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\salelog.ldf',
    SIZE = 5MB,
    MAXSIZE = 25MB,
    FILEGROWTH = 5MB ) ;
GO

You can add it into a SqlCommand. You will need an SqlConnection which I see you have. Hope it helps.

Nițu Alexandru
  • 714
  • 1
  • 11
  • 33
  • 2
    This answer misses so much context, explanation and overall information that it can be considered plain wrong as it currently stands. In other words: That certainly isn't what the OP needs to execute on the server. *Something similar* might be *part* of the script he *might* want to execute. – Daniel Hilgarth Jul 16 '13 at 14:50
  • @DanielHilgarth ...and then there's creating the database with both data and log explicitly named inside `C:\Program Files`. Even if this *is* what the OP wants, there are so many ways in which that is wrong that I'm not even going to bother start enumerating them. – user Jul 16 '13 at 14:52
0

It seems like this is being made way more complicated than it needs to be if you're planning to use SQL server.

Your application offers the user a way to enter a SQL server instance location and user with admin rights.

You then have a class with various methods which create your database, create your tables etc.

So you would do: 1) If not exists create database X. 2) IF not exists create tables A B C etc 3) alter the tables to setup the relations 4) If not exists create stored proc spA spB etc etc

and just build up the database that way.

Each step above would be a separate method which executes some inline SQL.

If you write the SQL to always check if the thing you're going to create exists it can be used to upgrade as well as create.

Purplegoldfish
  • 5,268
  • 9
  • 39
  • 59