Connecting to MS-Access

Connecting to MS-Access

OR_AND_NO
Advocate Advocate
872 Views
5 Replies
Message 1 of 6

Connecting to MS-Access

OR_AND_NO
Advocate
Advocate

Hi there!

I ve read a lot of topic about trable connection to MS Access. It is not getting clear.

I have a small function:

static void Access()
{
string DataBase = @"C:\Users\UserName\Documents\Database1.accdb";// and the only one simple table inside

// Connection string and SQL query
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DataBase;
string strSQL = "SELECT * FROM tbl1 order by Id";
// Create a connection
try
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{

// Open the connection and execute the select command.
// Open connecton
connection.Open();
if (connection.State != System.Data.ConnectionState.Open)
{ return; }
// Create a command and set its connection
using (OleDbCommand command = new OleDbCommand(strSQL, connection))
{

// Execute command
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
string Name = (string)reader["Name"];
int Id = (int)reader["Id"];
}
}
}
}
}
catch (OleDbException ex)
{
string msg = ex.Message;
throw;
}

}

 

This function works in simple program

static void Main(string[] args)
{
Access();
}

 

But kills revit without any exception here:

public Result Execute(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, ElementSet elements)
{
try
{
Access();
return Result.Succeeded;
}
catch (Exception ex)
{
string str = ex.Message;
throw ;
}
}

Could anyone to explain why?

Thanks forward!

0 Likes
Accepted solutions (1)
873 Views
5 Replies
Replies (5)
Message 2 of 6

jeremy_tammik
Alumni
Alumni
Accepted solution

Yes, people have encountered such issues in the past. Please search this forum for existing solutions, e.g., searching for the term OleDbConnection:

  

https://forums.autodesk.com/t5/forums/searchpage/tab/message?advanced=false&allow_punctuation=false&...

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 6

OR_AND_NO
Advocate
Advocate

Dear Jeremy thanks a lot for Your quick answer!

 

0 Likes
Message 4 of 6

OR_AND_NO
Advocate
Advocate

The right answer is here https://forums.autodesk.com/t5/revit-api-forum/cant-make-connection-to-acces-database/m-p/10879626

Thanks Jeremy!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

0 Likes
Message 5 of 6

jeremy_tammik
Alumni
Alumni

Thank you for your appreciation and pointing to the right answer. Congratulations on solving it!

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 6 of 6

jeremy_tammik
Alumni
Alumni

Since this seems to be a repeating question, I added it to the blog as well:

  

https://thebuildingcoder.typepad.com/blog/2023/09/add-in-threads-and-geometry-comparison.html#4

  

Thank you!

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes