0

I have installed Visual Studio Ultimate, and installed the Microsoft SQL Server, and tried to find my way around it, using some tutorials I found on line.
I have successfully compiled and run the following C# code:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection sql = new SqlConnection("Data Source=.\\SQLEXPRESS;Integrated Security=True;User Instance=True");
            sql.Open();
            SqlCommand command = new SqlCommand("CREATE DATABASE newDatabase;", sql);
            command.ExecuteNonQuery();

            command.CommandText = "CREATE TABLE newTable (name VARCHAR(20), age INT)";
            command.ExecuteNonQuery();

            command.CommandText = "INSERT INTO newTable VALUES ('John', 29)";
            command.ExecuteNonQuery();

            command.CommandText = "INSERT INTO newTable VALUES ('Jack', 21)";
            command.ExecuteNonQuery();

            command.CommandText = "INSERT INTO newTable VALUES ('Robin', 22)";
            command.ExecuteNonQuery();

            command.CommandText = "SELECT * FROM newTable;";
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                Console.WriteLine("{0} is {1} years old.",reader.GetString(0), reader.GetValue(1));
            }

            reader.Close();
            sql.Close();

            Console.ReadLine();

        }
    }
}

This will produce the right output, but now I want to actually see the newDatabase data-base. So I search for the keyword 'sql', and found the 'Microsoft SQL Server Managment Studio', and opened it.
Unfortunately, I couldn't find my database there under Databases:
enter image description here Where is it hiding and how can I find it?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
so.very.tired
  • 2,958
  • 4
  • 41
  • 69
  • 1
    have you right clicked `Databases` and hit Refresh? – Jonesopolis May 15 '14 at 17:04
  • Your commands apart from the create database are executed against the MASTER database. Try to expand the System Databases – Steve May 15 '14 at 17:17
  • I have tried it, and there's no change. Funny thing is that I searched my hard drive for `newDatabase` and found out it sits at `C:\Users\**my_user_name**\AppData\Local\Microsoft\Microsoft SQL Server Data\SQLEXPRESS` (as an `.mdf` file). Is it possible I have two copies of SQL on my PC that might cause this confusion? (because I remember that I installed it and then reinstalled it...) – so.very.tired May 15 '14 at 17:18

1 Answers1

1

You need to attach this new database. On Databases, "right-click", and attach Database. Browse to the C# project folder defined when you created your project with visual studio and you will find it in the folder.

Update

In case you want to define the path directly to avoid searching you can have look on this example which might help you:

  String str;
  SqlConnection myConn = new SqlConnection ("Data Source=.\\SQLEXPRESS;Integrated Security=True;User Instance=True");

  str = "CREATE DATABASE MyDatabase ON PRIMARY " +
       "(NAME = MyDatabase_Data, " +
       "FILENAME = 'C:\\MyDatabaseData.mdf', " +
       "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
       "LOG ON (NAME = MyDatabase_Log, " +
       "FILENAME = 'C:\\MyDatabaseLog.ldf', " +
       "SIZE = 1MB, " +
       "MAXSIZE = 5MB, " +
       "FILEGROWTH = 10%)";

  SqlCommand myCommand = new SqlCommand(str, myConn);
  try 
  {
    myConn.Open();
    myCommand.ExecuteNonQuery();

   }
  catch (System.Exception ex)
  {
    Console.Write(ex.ToString());
  }
  finally
  {
    if (myConn.State == ConnectionState.Open)
    {
    myConn.Close();
    }
  }
  • I right-clicked it and chose 'attach', and got new "Attach Databases" window. I then clicked "Add.." and a navigate widow opened up. I then tried to navigate to my VS C# project path, but couldn't (it won't expand the `c:\users\**my_username**` folder. so, I tried to navigate my VS C# project path through the Windows Explorer window (the usual way of navigating folders in Windows), and found out that the folder `c:\users\**my_username**` icon has a lock image on it. I guess this is why it won't let me expand it when trying to navigate it through the "Attach Databases" window. – so.very.tired May 15 '14 at 17:41
  • nevertheless, my C# project folder has no `.mdf` file in it... though I found my `.mdf` file at `C:\Users\**my_user_name**\AppData\Local\Microsoft\Microsoft SQL Server Data\SQLEXPRESS` for some reason... – so.very.tired May 15 '14 at 17:41
  • Sure you right, as you didn't precise the path I presume. –  May 15 '14 at 17:51
  • Sorry, I don't understand, are you saying the path is wrong? – so.very.tired May 15 '14 at 17:55
  • OK, so the Update you added to your post will enable me to navigate to the .mdf file and attach it? – so.very.tired May 15 '14 at 18:53