I tried it today. In DNN mode, some modules use mysql to store data, while the primary DNN still runs under SQL Server.
The procedure is as follows:
1. Add a connection in web. config:
<Add
Name = "SiteMySqlServer"
ConnectionString = "Server = 192.168.69.159; Port = 3306; Database = Demo; Uid = root; Pwd ="
ProviderName = "System. Data. SqlClient"/>
* ProviderName -- ignore.
It is mainly connectionString. It is very strange at present that charset cannot be added in it. Once added, mysql connector will report an error.
2. Introduce mysql connector <detailed: http://dev.mysql.com/downloads/connector/net/6.0.html>, the latest version is 6.0
3. Introduce mysql. data in your dnn module. The main class we use is MySqlHelper.
The related instance code is as follows, which includes the direct use of SQL statements, call of stored procedures, and whether to include parameters. If you need to use transactions, try to use the stored procedure mode. MySqlHelper does not seem to be able to directly support transaction operations.
Code
Public override IDataReader GetUserList ()
{
MySqlParameter param1 = new MySqlParameter ();
Param1.Value = "xxx ";
Param1.ParameterName = "name ";
MySqlParameter param2 = new MySqlParameter ();
Param2.Value = 20;
Param2.ParameterName = "age ";
// Return MySqlHelper. ExecuteReader (_ connectionString, "select * from test where name = @ name and age = @ age", new MySqlParameter [] {param1, param2 });
Return MySqlHelper. ExecuteReader (_ connectionString, "call test_sp_getuser (@ name, @ age)", new MySqlParameter [] {param1, param2 });
}
4. entity class (PO ),
In general, we use CBO. FillCollection <UserInfo> (DataProvider. Instance (). GetUserList (); to fill in data. However, in actual testing, this method does not work.
To support this method, we must implement the IHydratable interface in the UserInfo class. The instance code is as follows:
Code
Public class UserInfo: IHydratable
{
Public int Id;
Public string Name;
Public int Age;
# Region IHydratable Member
Public void Fill (System. Data. IDataReader dr)
{
Id = Convert. ToInt32 (Null. SetNull (dr ["Id"], Id ));
Name = Convert. ToString (Null. SetNull (dr ["name"], Name ));
Age = Convert. ToInt32 (Null. SetNull (dr ["age"], Age ));
}
Public int KeyID
{
Get
{
Return Id;
}
Set
{
Id = value;
}
}
# Endregion
}
Story is over ....
Enjoy yourself.