You can't really create a database in your solution files as an SQL database: if doesn't quite work like that. SQL Server decides where and how the database is physically located, as it needs to access it (as far as it knows) from multiple clients.
If you could create an SQL database on your machine, then what would happen when your installation program is run on a different machine, using the same SQL Server instance? Are they supposed to share the information? If so, how is that going to work when SQL server can't guarantee that either or both of the machines are powered up?
If you must create it locally, consider using SQLCE or Sqlite instead: these are single user databases with most if not all the SQL server functionality, but designed to be run on each machine separately.
For SqlCe, the code is simple:
private void RebuildDB()
{
if (!File.Exists(strDB))
{
try
{
SqlCeEngine engine = new SqlCeEngine(strCon);
engine.CreateDatabase();
Log.AddLog("DataBase Created");
}
catch (System.Exception ex)
{
Log.AddLog("Error creating database: " + ex.ToString());
}
}
using (SqlCeConnection con = new SqlCeConnection(strCon))
{
con.Open();
SqlCeCommand cmd = new SqlCeCommand("SELECT table_name FROM INFORMATION_SCHEMA.TABLES", con);
SqlCeDataReader r = cmd.ExecuteReader();
Log.AddLog("Reading Tables");
bool tablePresent = false;
while (r.Read())
{
string tableName = (string) r[0];
if (tableName == strTable)
{
tablePresent = true;
}
}
if (tablePresent)
{
Log.AddLog("Removing table");
cmd = new SqlCeCommand(string.Format("DROP TABLE {0}", strTable), con);
cmd.ExecuteNonQuery();
}
Log.AddLog("Creating table");
cmd.Dispose();
cmd = new SqlCeCommand(string.Format("CREATE TABLE {0} (id int NOT NULL, name nvarchar(100), level nvarchar(10), password nvarchar(100))", strTable), con);
cmd.ExecuteNonQuery();
cmd.Dispose();
cmd = new SqlCeCommand(string.Format("CREATE UNIQUE INDEX idx1 ON {0} (id)", strTable), con);
cmd.ExecuteNonQuery();
cmd.Dispose();
cmd = new SqlCeCommand(string.Format("INSERT INTO {0} (id, name, level, password) VALUES (@ID, @NM, @LV, @PW)", strTable), con);
cmd.Parameters.AddWithValue("@ID", 1);
cmd.Parameters.AddWithValue("@NM", "Joe Green");
cmd.Parameters.AddWithValue("@LV", "User");
cmd.Parameters.AddWithValue("@PW", "JoePass");
cmd.ExecuteNonQuery();
cmd.Parameters["@ID"].Value = 2;
cmd.Parameters["@NM"].Value = "Pete Smith";
cmd.Parameters["@LV"].Value = "User";
cmd.Parameters["@PW"].Value = "PetePass";
cmd.ExecuteNonQuery();
cmd.Parameters["@ID"].Value = 3;
cmd.Parameters["@NM"].Value = "Miss Scarlet";
cmd.Parameters["@LV"].Value = "User";
cmd.Parameters["@PW"].Value = "MissPass";
cmd.ExecuteNonQuery();
cmd.Parameters["@ID"].Value = 4;
cmd.Parameters["@NM"].Value = "Mike Black";
cmd.Parameters["@LV"].Value = "User";
cmd.Parameters["@PW"].Value = "MikePass";
cmd.ExecuteNonQuery();
cmd.Parameters["@ID"].Value = 5;
cmd.Parameters["@NM"].Value = "The Boss";
cmd.Parameters["@LV"].Value = "Admin";
cmd.Parameters["@PW"].Value = "18d8bc8c-cb91-417e-8a63-28c8ab0a6988";
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
}