Hi all,
I am trying to connect .NET to a MySql Database with VS2010 and .net framework 4.0.
1) First of all, I've tried the classic connection to the DB and it works correctly.
This is the code:
**************************************************************
public Actor GetActorByID(int id)
{
try
{
Actor myActor = null;
StringBuilder SQLBuilder = new StringBuilder();
SQLBuilder.Append(" SELECT * from actor");
SQLBuilder.Append(" WHERE id = :id ");
DbCommand cmd = defaultDB.GetSqlStringCommand(SQLBuilder.ToString());
defaultDB.AddInParameter(cmd, "id", DbType.Int32, id);
using (IDataReader reader = defaultDB.ExecuteReader(cmd))
{
if (reader.Read())
{
myActor = new Actor();
myActor.id = (reader["ID"] != null) ? int.Parse(reader["ID"].ToString()) : -1;
myActor.Name = (reader["NAME"] != null) ? reader["NAME"].ToString().Trim() : string.Empty;
myActor.Surname = (reader["SURNAME"] != null) ? reader["SURNAME"].ToString().Trim() : string.Empty;
}
try
{
reader.Close();
}
finally { }
}
return myActor;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
**************************************************************
Everything works in the above way.
2) Now, I tried to connect via Enterprise Library. Here is the code:
web.config
**************************************************************
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
</configSections>
<dataConfiguration defaultDatabase="sakila-Svil1" />
<connectionStrings>
<!-- dev -->
<add name="sakila-Svil1" connectionString="server=localhost;user id=root;password=root; persistsecurityinfo=True; database=sakila" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
...etc etc etc...
</configuration>
***********************************************************
Note: the connection string is the same as the working solution.
Code:
***********************************************************
protected static DatabaseProviderFactory dataFactory;
protected static Database defaultDB;
dataFactory = new DatabaseProviderFactory();
defaultDB = EnterpriseLibraryContainer.Current.GetInstance<Database>();
***********************************************************
When I try to "EnterpriseLibraryContainer.Current.GetInstance<Database>();" the following error appears: {"Activation error occured while trying to get instance of type Database, key \"\""}
I referenced in the project and copied into the bin folder:
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Data.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Configuration.dll
Microsoft.Practices.Unity.Interception.dll
Microsoft.Practices.Unity.Interception.Configuration.dll
mysql.data.dll
mysql.web.dll
To be sure, I also registered into the GAC mysql.data.dll and mysql.web.dll, but nothing happened. Everytime I try to execute:EnterpriseLibraryContainer.Current.GetInstance<Database>(), the error occours.
I also tried to give the database name as parameter in the GetInstance<Database> method but the result is always the same.
I tried also to add an SqlDataSource component on my aspx page. I activated the wizard to configure the database connection entering the following parameters:
Data Source: MySqlDatabase
Data Provider: .NET Framework Data Provider for MySql
Server name: localhost
User name: root
Passrowd: root
Database Name: sakila (selected by the dropdown list)
After closing and clicking the next button, the following messagebox error appears:
the data provider 'mysql.data.mysqlclient' could not be found in the system configuration"
Anyone can help me?
Thanx very much.
Claudio
I am trying to connect .NET to a MySql Database with VS2010 and .net framework 4.0.
1) First of all, I've tried the classic connection to the DB and it works correctly.
This is the code:
**************************************************************
public Actor GetActorByID(int id)
{
try
{
Actor myActor = null;
StringBuilder SQLBuilder = new StringBuilder();
SQLBuilder.Append(" SELECT * from actor");
SQLBuilder.Append(" WHERE id = :id ");
DbCommand cmd = defaultDB.GetSqlStringCommand(SQLBuilder.ToString());
defaultDB.AddInParameter(cmd, "id", DbType.Int32, id);
using (IDataReader reader = defaultDB.ExecuteReader(cmd))
{
if (reader.Read())
{
myActor = new Actor();
myActor.id = (reader["ID"] != null) ? int.Parse(reader["ID"].ToString()) : -1;
myActor.Name = (reader["NAME"] != null) ? reader["NAME"].ToString().Trim() : string.Empty;
myActor.Surname = (reader["SURNAME"] != null) ? reader["SURNAME"].ToString().Trim() : string.Empty;
}
try
{
reader.Close();
}
finally { }
}
return myActor;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
**************************************************************
Everything works in the above way.
2) Now, I tried to connect via Enterprise Library. Here is the code:
web.config
**************************************************************
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
</configSections>
<dataConfiguration defaultDatabase="sakila-Svil1" />
<connectionStrings>
<!-- dev -->
<add name="sakila-Svil1" connectionString="server=localhost;user id=root;password=root; persistsecurityinfo=True; database=sakila" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
...etc etc etc...
</configuration>
***********************************************************
Note: the connection string is the same as the working solution.
Code:
***********************************************************
protected static DatabaseProviderFactory dataFactory;
protected static Database defaultDB;
dataFactory = new DatabaseProviderFactory();
defaultDB = EnterpriseLibraryContainer.Current.GetInstance<Database>();
***********************************************************
When I try to "EnterpriseLibraryContainer.Current.GetInstance<Database>();" the following error appears: {"Activation error occured while trying to get instance of type Database, key \"\""}
I referenced in the project and copied into the bin folder:
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Data.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Configuration.dll
Microsoft.Practices.Unity.Interception.dll
Microsoft.Practices.Unity.Interception.Configuration.dll
mysql.data.dll
mysql.web.dll
To be sure, I also registered into the GAC mysql.data.dll and mysql.web.dll, but nothing happened. Everytime I try to execute:EnterpriseLibraryContainer.Current.GetInstance<Database>(), the error occours.
I also tried to give the database name as parameter in the GetInstance<Database> method but the result is always the same.
I tried also to add an SqlDataSource component on my aspx page. I activated the wizard to configure the database connection entering the following parameters:
Data Source: MySqlDatabase
Data Provider: .NET Framework Data Provider for MySql
Server name: localhost
User name: root
Passrowd: root
Database Name: sakila (selected by the dropdown list)
After closing and clicking the next button, the following messagebox error appears:
the data provider 'mysql.data.mysqlclient' could not be found in the system configuration"
Anyone can help me?
Thanx very much.
Claudio