3

I'm trying to connect to a database via C#, but I'm getting a very unhelpful error message when I do so:

"08:44:17: Error: Cannot initialize OLE 08:44:17: Error: Cannot initialize OLE"

I've tried looking for a solution, but I've been unsuccessful. I also tried restarting my computer, which didn't help either.

I am running SQL Server 2008, and here is the relevant database code:

/// <summary>
    /// Connects to a given database and returns the database connection.
    /// </summary>
    /// <param name="file">The database file name.</param>
    /// <returns>The database connection.</returns>
    public static SqlConnection ConnectToDb(string file)
    {
        //initialize generic path
        string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
        path = path.Replace("bin\\Debug\\MediaPlayer.exe", "");
        path += "Database.mdf";

        string connectionPath = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + path + ";Integrated Security=True;User Instance=True";
        SqlConnection connection = new SqlConnection(connectionPath);
        return connection;
    }

    /// <summary>
    /// Executes a SQL query in a given database.
    /// </summary>
    /// <param name="file">The database file name.</param>
    /// <param name="query">The SQL query to execute.</param>
    public static void ExecuteQuery(string file, string query)
    {

        SqlConnection connection = ConnectToDb(file);
        connection.Open();
        SqlCommand command = new SqlCommand(query, connection);
        command.ExecuteNonQuery();
        connection.Close();
    }

This is database code that I have used for several project, and it has always worked before.

The error is called (I know this because I commented out other lines) on the connection.Open() line in the ExecuteQuery method.

Any help/advice would be greatly appreciated :).

EDIT: I tested my connection to the database and everything checked out, I just don't understand why I can't connect via code.

Daniel
  • 2,944
  • 3
  • 22
  • 40
  • 1st off... open SSMS and try to connect to sqlexpress. – RoMEoMusTDiE May 12 '12 at 05:53
  • How do I open SSMS? Sorry, I'm inexperienced :P.. It doesn't appear as a program nor a toolbar in Visual Studio.. – Daniel May 12 '12 at 06:12
  • SSMS is the user interface shipped with the sql server. So you open Microsoft SQL Management Studio, connect to your server and see if the sql server works in general. – YvesR May 12 '12 at 06:55
  • @Daniel why you connect like this and not to the sql server directly? – YvesR May 12 '12 at 06:57

2 Answers2

2

For anyone who might be looking, I eventually solved this by adding a [STAThread()] before the Main() method. :)

Daniel
  • 2,944
  • 3
  • 22
  • 40
0

I suspect that you are building the project for x64 CPU. There is no OLE driver for x64. I recommend to change the build target from ANY CPU to x86.

Svetlin Ralchev
  • 614
  • 6
  • 5
  • Maybe I misunderstood, but I opened the project properties and opened the "Build" panel, and under Platform the selected option is "Active (x86)".. In fact, that's the only option. – Daniel May 12 '12 at 06:21
  • I'm running 64-bit Windows 7. – Daniel May 12 '12 at 07:10