Message 1 of 11
Cant make connection to acces database
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello
I can't make a connection to an Acces database in Revit.
Every time I use "connection.Open();" Revit crashes without a warning or error message.
Here is the code I used to test if I can make a connection to Acces database:
namespace TestDatabaseConnection
{
[Transaction(TransactionMode.Manual)]
public class TestDatabaseConnection : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
// Connection string and SQL query
string connectionString = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" +
@"F:\Revit\Families_Codraft\Pipe Fittings\COD_Y-STUK\Test\BMPfittings_PE_riol.mdb";
string strSQL = "SELECT *" +
" FROM BMP_TeeTbl" +
" WHERE (((BMP_TeeTbl.PIPE_OD_M)=110)" +
" AND ((BMP_TeeTbl.PIPE_OD_R)=110)" +
" AND ((BMP_TeeTbl.PIPE_OD_B)=110)" +
" AND ((BMP_TeeTbl.Angle)=45)" +
" AND ((BMP_TeeTbl.EndType)=\"-;-;-;\")" +
" AND ((BMP_TeeTbl.THD_ENG1)=0)" +
" AND ((BMP_TeeTbl.THD_ENG2)=0)" +
" AND ((BMP_TeeTbl.THD_ENG3)=0))";
// Create a connection
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
// Create a command and set its connection
OleDbCommand command = new OleDbCommand(strSQL, connection);
// Open the connection and execute the select command.
try
{
// Open connecton
connection.Open();
// Execute command
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
TaskDialog.Show("test", reader["Object"].ToString() + " " + reader["COMP_LEN"].ToString());
}
}
return Result.Succeeded;
}
catch (Exception ex)
{
TaskDialog.Show("test",ex.Message);
return Result.Failed;
}
// The connection is automatically closed becasuse of using block.
}
}
}
}
If I test it with a Console App it works perfectly fine.