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 
        }
6
  • Entity Framework Code First? Commented Jul 16, 2013 at 14:42
  • Your question is not clear. Commented Jul 16, 2013 at 14:42
  • Good point to start would be determine what database you mean ? Relation ? File ? or other. Commented Jul 16, 2013 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 msdn.microsoft.com/en-us/library/ms176061.aspx Commented Jul 16, 2013 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 Commented Jul 16, 2013 at 14:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
@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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.