Click here to Skip to main content
15,741,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been working on this project for quite a time now, i am trying to create my database within a folder called "Database" in my solution. (This is a windows application) i am having problem specifically with my connection string. Currently this is what i have
HTML
<add name="investment.My.MySettings.investmentConnectionString1"
     connectionString="Data Source=*****\SQLEXPRESS;Initial Catalog=investment;Integrated Security=True"
     providerName="System.Data.SqlClient" /> 

but when ever I run the application I get an unhandled exception. Please how can I create a database withing my solution folder and attach it to an SQL Server Instance so that i can create more database object.
Posted
Updated 12-Sep-11 10:09am
v4
Comments
Herman<T>.Instance 12-Sep-11 13:47pm    
what code gives the exception?

1 solution

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:

C#
/// <summary>
/// Rebuild the database to a known, safe state.
/// </summary>
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();
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900