In a small application I’m making for school, I’m attempting to determine if the necessary database exists.
If it does not, I want to create it (along with the tables in the database), and then use it as normal.
I’m using Visual C# Express Edition, along with SQL Server Express. I wrote the small test program below…
namespace DatabaseConnectionTest
{
class Program
{
public static SqlConnection con;
public static void EstablishConnection()
{
string userName = "username";
string password = "password";
string server = @".\SQLEXPRESS";
string database = "Blibbity";
string trustedConnection = "yes";
string timeout = "30";
try
{
con = new SqlConnection(
"user id=" + userName +
";password=" + password +
";server=" + server +
";Trusted_Connection=" + trustedConnection +
";database=" + database +
";connection timeout=" + timeout);
con.Open();
Console.WriteLine("Connection successful!");
var command = new SqlCommand("insert into sometable values ('somedata')", con);
command.ExecuteNonQuery();
Console.WriteLine("Insert successful!");
command = new SqlCommand("select somecolumn from sometable", con);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("Fectched data ====> " + reader["somecolumn"].ToString());
}
reader.Close();
Console.WriteLine("Query successful!");
command = new SqlCommand("delete from sometable where somecolumn = 'somedata'", con);
command.ExecuteNonQuery();
Console.WriteLine("Delete successful!");
}
catch
{
database = "master";
con = new SqlConnection(
"user id=" + userName +
";password=" + password +
";server=" + server +
";Trusted_Connection=" + trustedConnection +
";database=" + database +
";connection timeout=" + timeout);
con.Open();
var command = new SqlCommand(@"
USE [master]
GO
/****** Object: Database [Blibbity] Script Date: 04/12/2012 07:08:45 ******/
CREATE DATABASE [Blibbity] ON PRIMARY
( NAME = N'Blibbity', FILENAME = N'c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\Blibbity.mdf' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'Blibbity_log', FILENAME = N'c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\Blibbity_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [Blibbity] SET COMPATIBILITY_LEVEL = 100
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [Blibbity].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [Blibbity] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [Blibbity] SET ANSI_NULLS OFF
GO
ALTER DATABASE [Blibbity] SET ANSI_PADDING OFF
GO
ALTER DATABASE [Blibbity] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [Blibbity] SET ARITHABORT OFF
GO
ALTER DATABASE [Blibbity] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [Blibbity] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [Blibbity] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [Blibbity] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [Blibbity] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [Blibbity] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [Blibbity] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [Blibbity] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [Blibbity] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [Blibbity] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [Blibbity] SET DISABLE_BROKER
GO
ALTER DATABASE [Blibbity] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [Blibbity] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [Blibbity] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [Blibbity] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [Blibbity] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [Blibbity] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [Blibbity] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [Blibbity] SET READ_WRITE
GO
ALTER DATABASE [Blibbity] SET RECOVERY SIMPLE
GO
ALTER DATABASE [Blibbity] SET MULTI_USER
GO
ALTER DATABASE [Blibbity] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [Blibbity] SET DB_CHAINING OFF
GO", con);
command.ExecuteNonQuery();
con.Close();
database = "Blibbity";
con = new SqlConnection(
"user id=" + userName +
";password=" + password +
";server=" + server +
";Trusted_Connection=" + trustedConnection +
";database=" + database +
";connection timeout=" + timeout);
con.Open();
command = new SqlCommand(@"
USE [Blibbity]
GO
/****** Object: Table [dbo].[sometable] Script Date: 04/12/2012 07:09:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[sometable](
[somecolumn] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO", con);
command.ExecuteNonQuery();
con.Close();
EstablishConnection();
}
finally
{
con.Close();
Console.WriteLine("Connection now closed...");
Console.ReadLine();
}
}
static void Main(string[] args)
{
EstablishConnection();
}
}
}
Obviously Blibbity is just a junk database. It’s when I hit the first ExecuteNonQuery() line in the exception catch, it tells me that my syntax near "GO" is incorrect, yet I just copied the text for the database/table creation using SQL Server Management Studio’s “Script As CREATE TO” feature.
Does anybody know why I’m running into this issue?
Many thanks.
GO is not a sql server command, it is interpreted by Management studio. If you want to achieve the same behavior, than you have to split the sql command by the go instructions and execute each part sequentially.