How to create database and tables on SQL Server from scratch or programmatically?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I created a database("parv_address_book") and a table("Contacts") in it, using SSMS. After that I generated T-SQL script using the same, which is
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; IF <span style="color:Blue; NOT <span style="color:Blue; EXISTS (<span style="color:Blue; SELECT <span style="color:Blue; name <span style="color:Blue; FROM sys.databases <span style="color:Blue; WHERE <span style="color:Blue; name = N<span style="color:#A31515; parv_address_book)
<span style="color:Blue; BEGIN
<span style="color:Blue; CREATE <span style="color:Blue; DATABASE [parv_address_book]
<span style="color:Blue; END

<span style="color:Blue; IF <span style="color:Blue; NOT <span style="color:Blue; EXISTS (<span style="color:Blue; SELECT * <span style="color:Blue; FROM sys.objects <span style="color:Blue; WHERE <span style="color:Blue; object_id = <span style="color:Blue; OBJECT_ID(N<span style="color:#A31515; [Contacts]) <span style="color:Blue; AND <span style="color:Blue; type <span style="color:Blue; in (N<span style="color:#A31515; U))
<span style="color:Blue; BEGIN
<span style="color:Blue; CREATE <span style="color:Blue; TABLE Contacts(
[ID] [int] <span style="color:Blue; IDENTITY(1,1) <span style="color:Blue; NOT <span style="color:Blue; NULL,
[First name] [nvarchar](100) <span style="color:Blue; NOT <span style="color:Blue; NULL,
[Last name] [nvarchar](100) <span style="color:Blue; NOT <span style="color:Blue; NULL,
[Address] [nvarchar](255) <span style="color:Blue; NOT <span style="color:Blue; NULL,
[Telephone] [nvarchar](20) <span style="color:Blue; NULL,
[nvarchar](100) <span style="color:Blue; NULL,
[Mobile] [nvarchar](20) <span style="color:Blue; NULL,
<span style="color:Blue; CONSTRAINT [PK_Contacts] <span style="color:Blue; PRIMARY <span style="color:Blue; KEY <span style="color:Blue; CLUSTERED
([ID] <span style="color:Blue; ASC) <span style="color:Blue; WITH (PAD_INDEX = <span style="color:Blue; OFF, STATISTICS_NORECOMPUTE = <span style="color:Blue; OFF, IGNORE_DUP_KEY = <span style="color:Blue; OFF, ALLOW_ROW_LOCKS = <span style="color:Blue; ON, ALLOW_PAGE_LOCKS = <span style="color:Blue; ON))
<span style="color:Blue; END
[/code]
After that I deleted the database.
Now, when I try to create the database and table using this program,
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using (SqlConnection conn = <span style="color:Blue; new SqlConnection(<span style="color:#A31515; "Data Source=(local);Initial Catalog=master;Integrated Security=SSPI;"))
{
<span style="color:Blue; try
{
conn.Open();
<span style="color:Blue; string sql = <span style="color:#A31515; "IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = Nparv_address_book)"
+ <span style="color:#A31515; "BEGIN CREATE DATABASE [parv_address_book] END;";
SqlCommand cmd = <span style="color:Blue; new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();

sql = <span style="color:#A31515; "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N[Contacts]) AND type in (NU))"
+ <span style="color:#A31515; "BEGIN CREATE TABLE Contacts("
+ <span style="color:#A31515; "[ID] [int] IDENTITY(1,1) NOT NULL,"
+ <span style="color:#A31515; "[First name] [nvarchar](100) NOT NULL,"
+ <span style="color:#A31515; "[Last name] [nvarchar](100) NOT NULL,"
+ <span style="color:#A31515; "[Address] [nvarchar](255) NOT NULL,"
+ <span style="color:#A31515; "[Telephone] [nvarchar](20) NULL,"
+ <span style="color:#A31515; "[Email] [nvarchar](100) NULL,"
+ <span style="color:#A31515; "[Mobile] [nvarchar](20) NULL,"
+ <span style="color:#A31515; "CONSTRAINT [PK_Contacts] PRIMARY KEY CLUSTERED"
+ <span style="color:#A31515; "([ID] ASC)"
+ <span style="color:#A31515; "WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)"
+ <span style="color:#A31515; ") END;";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}

<span style="color:Blue; catch (Exception ex)
{
MessageBox.Show(<span style="color:#A31515; "Error occured while creating database and table.n"
+ ex.Message, <span style="color:Blue; this.Text);
}
}
[/code]
It only creates the database and not the table. Also, no exception is thrown.
Thank you. <hr class="sig Believes in simplicity, speed and stability.

[url=http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/54a655a5-9696-4aa1-a53a-5d4267bccc0d]View the full article[/url]
 
Back
Top