I'll prelude this with I am a novice coder at best and this is mostly a hobby that I would like to do professionally one day, but that day may be 10+ years from now. So for now I tinker and learn here and there. Part of the reason I am using this design is to gain experience and understanding. Id like to start it off as a local database, then across my network, then the internet, then different users with different permission levels and different database types..etc. As for now though, this proverbial rocket wont even hop, muchless take off and land on the moon.
As the title suggests, I am trying to create a database from within my C# code on a windows form. I will then naturally create and use tables..etc. However, I am stuck on the initial creation of a blank database. Based off of "SQL Server Management Studio 2010" its (Microsoft SQL Server 2008) SQL Server Compact Edition database. I am coding with Visual Studio Ultimate 2010.
So part of my problem could be I am trying to create the wrong type of database? Or pairing the wrong syntax with the wrong database? I know that access is a file based DB and SQL is server based and that I am basically trying to create a local server database in this scenario.
I have the following code set to run the below code.
using System.Data.SqlClient;
SqlConnection myConn = new SqlConnection("server=(local);database=Master;integrated security=SSPI;");
string str;
str = "CREATE DATABASE MyDatabase";
SqlCommand myCommand = new SqlCommand(str, myConn);
try
{
myConn.Open();
MessageBox.Show("Connection open");
myCommand.ExecuteNonQuery();
MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
if (myConn.State == ConnectionState.Open)
{
myConn.Close();
}
}
The abridged version of the error is "System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while stablishing a connection to SQL Server. The server was not found or was not accessible." I am trying to create a local database, so it shouldn't be a firewall issue? I went ahead and opened port 1433 anyway. I havnt yet tried using my own IP though. And I never get my 'connection open' messagebox, so that definitely narrows it to the connection string.
I've tried countless variations of the connection string without success. Below are some of the words/spellings/syntax I have swapped out throughout my various attempts, obviously not written as a proper string. After 3 days on and off of this and countless googling attempts, I am out of ideas.
server="local" "(local)" "." <user> <pcname> ".\SQL2008" ".\SQLEXPRESS"
"integrated security"="true" "yes" or "trusted_connection"="true" "yes"
database="master" database=""