I get an “Error: Cannot initialize OLE” when I try to connect to a database? C#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; text-align:left
Im trying to connect to a database via C#, but Im getting a very unhelpful error message when I do so:
<blockquote style="margin-bottom:10px; padding-bottom:1px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; background-color:#eeeeee; quotes:none; font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; text-align:left
<p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; vertical-align:baseline; background-color:transparent; clear:both; word-wrap:break-word
08:44:17: Error: Cannot initialize OLE 08:44:17: Error: Cannot initialize OLE
</blockquote>
<p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; text-align:left
Ive tried looking for a solution, but Ive been unsuccessful. I also tried restarting my computer, which didnt help either.
<p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; text-align:left
I am running SQL Server 2008, and here is the relevant database code:
<pre class="x_x_default x_x_prettyprint" style="padding-bottom:5px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; background-color:#eeeeee; font-family:Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,serif; overflow-x:auto; overflow-y:auto; width:auto; max-height:600px; text-align:left <code style="margin-bottom:0px; padding-bottom:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; font-family:Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,serif <span class="pln" style="margin-bottom:0px; padding-bottom:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; vertical-align:baseline; background-color:transparent <pre class="prettyprint /// <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();
}[/code]<br/><span style="color:#808080 <br/>[/code][/code]
<p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; text-align:left
This is database code that I have used for several project, and it has always worked before.
<p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; text-align:left
The error is called (I know this because I commented out other lines) on the connection.Open() line in the ExecuteQuery method.
<p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; text-align:left
Any help/advice would be greatly appreciated :).
<p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; text-align:left

<p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; text-align:left
Edit: Some potentially relevant information - the path is fine and correct, thats definitely not the issue. Im running 64-bit Windows 7, and the Active Platform selection is set to x86.
<br/>

View the full article
 
Back
Top