DNN and Mysql. Net Connector

Source: Internet
Author: User
Tags dnn

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.