Create Database and table

Esther

I have to create a database and afterwards create a table within the database created.

I have connected to the master on my connection string, but and I want to add the new database name I have just created within the Initial Catalog when connecting and creating the table.

This is what I have

string conn = "SERVER=BRIAN-PC\\SQLEXPRESS; Initial Catalog=master; user id =sa; Password=kagiso";

            String str = "CREATE DATABASE IF NOT EXIST PSAHoldings ON PRIMARY"
                + "(NAME = PSAHoldings_Data,"
                + "FILENAME = 'C:\\PSAHoldings.mdf'',"
                + "SIZE = 2MB, FILEGROWTH =10%)"
                + "LOG ON (NAME = PSAHoldings_Log,"
                + "FILENAME = 'C:\\PSAHoldingsLog,idf',"
                + "SIZE = 1MB,"
                + "FILEGROTH = 10%)";

SqlConnection connection = new SqlConnection(conn);
            connection.Open();

try
            {
                //SqlCommand to create database
                SqlCommand cmd = new SqlCommand(str, connection);
                cmd.ExecuteNonQuery();

                MessageBox.Show("DataBase was successfully created", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

string connect = "Data Source=Esther-PC\\SQLEXPRESS; Initial Catalog=PSAHoldings; user id =sa; Password=kagiso";

            string table = "CREATE TABLE IF NOT EXIST t_original (" +
                "empId varChar(10) NOT NULL PRIMARY KEY," +
                "paycode varChar(10) NOT NULL," +
                "amount int NOT NULL," +
                ")";


            SqlConnection con = new SqlConnection(connect);
            con.Open();

            SqlCommand createTable = new SqlCommand(table, con);
            createTable.ExecuteNonQuery();
            }
}
            catch (SqlException sqlEx)
            {
                MessageBox.Show(sqlEx.ToString(), "Exception Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                connection.Close();
                con.Close();
            }

When I run the application I keep on getting exception errors.

The EXCEPTION ERROR reads it says System.Data.SqlClient.SqlException(0x80131904): Incorrect synax near the keyword 'IF'. expression of non-boolean type specified in a context where a condition is expected, near 'PSAHoldings', Unclosed quotation mark after the character string 'Size=1mb, filegrowth=10%)'.

colmde

I don't think there's such code as CREATE DATABASE IF NOT EXIST - Can't find any mention of it on MSDN: https://msdn.microsoft.com/en-us/library/ms176061.aspx

Try this.

IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'PSAHoldings')
  CREATE DATABASE PSAHoldings ON PRIMARY 
    (NAME = PSAHoldings_Data,
    FILENAME = 'C:\\PSAHoldings.mdf',
    SIZE = 3MB, FILEGROWTH =10%)
    LOG ON (NAME = PSAHoldings_Log,
    FILENAME = 'C:\\PSAHoldingsLog.idf',
    SIZE = 1MB,
    FILEGROWTH = 10%)

(Note some other mis-types fixed also changed file size to 3MB as I had an error saying it needed at least 3MB)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive