需要在Code中进行一些修改

时间:2015-05-20 13:19:16

标签: mysql asp.net asp.net-mvc database-connection reader

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace CollectDetails.Models
{
public class cPerson
{

    public string ideshot_bulk_offers { get; set; }
    public string ideshot_bulk_emails { get; set; }
    public string accomName { get; set; }
    public String tripadvisor { get; set; }
    public String travellerReviews { get; set; }
    public string resort { get; set; }
    public string region { get; set; }
    public String rating { get; set; }
    public string boardBasis { get; set; }
    public string departAirport { get; set; }
    public string month { get; set; }
    public String duration { get; set; }
    public String wasPrice { get; set; }
    public String nowPrice { get; set; }
    public string optStatement { get; set; }
    public string offerText { get; set; }
    public string optText { get; set; }
    public string fullDescription { get; set; }
    public string url { get; set; }
    public String ord { get; set; }

    //public int PersonID { get; set; }
    //public string Name { get; set; }
    //public string Address1 { get; set; }
    //public string Address2 { get; set; }
    //public byte[] Photo { get; set; }

    private bool connection_open;
    private MySqlConnection connection;

    public cPerson()
    {

    }

    public cPerson(int arg_limit)
    {
        Get_Connection();
       // PersonID = arg_i;
        //    m_Person = new CPersonMaster();
        //  List<CPersonMaster> PersonList = new List<CPersonMaster>();
        //PersonList = CComs_PM.Fetch_PersonMaster(connection, 4, arg_id);

        //if (PersonList.Count==0)
        //  return "";

        //m_Person = PersonList[0];

        //DB_Connect.CloseTheConnection(connection);
        try
        {
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = connection;
            cmd.CommandText =
        string.Format("SELECT t1.* FROM eshot_bulk_offers AS t1 WHERE t1.ideshot_bulk_emails IN (SELECT ideshot_bulk_emails FROM eshot_bulk_emails WHERE send_status = 'PROCESSED' AND basedOn = 0 ORDER BY ideshot_bulk_emails DESC) ORDER BY t1.ideshot_bulk_emails DESC, t1.ord LIMIT 2");

            MySqlDataReader reader = cmd.ExecuteReader();

            try
            {
                reader.Read();

                if (reader.IsDBNull(0) == false)
                    ideshot_bulk_offers = reader.GetString(0);
                else
                    ideshot_bulk_offers = null;

                if (reader.IsDBNull(1) == false)
                    ideshot_bulk_emails = reader.GetString(1);
                else
                    ideshot_bulk_emails = null;
                if (reader.IsDBNull(2) == false)
                    accomName = reader.GetString(2);
                else
                    accomName = null;
                if (reader.IsDBNull(3) == false)
                    tripadvisor = reader.GetString(3);
                else
                    tripadvisor = null;
                if (reader.IsDBNull(4) == false)
                    travellerReviews = reader.GetString(4);
                else
                    travellerReviews = null;
                if (reader.IsDBNull(5) == false)
                    resort = reader.GetString(5);
                else
                    resort = null;
                if (reader.IsDBNull(6) == false)
                    region = reader.GetString(6);
                else
                    region = null;
                if (reader.IsDBNull(7) == false)
                    rating = reader.GetString(7);
                else
                    rating = null;
                if (reader.IsDBNull(8) == false)
                    boardBasis = reader.GetString(8);
                else
                    boardBasis = null;
                if (reader.IsDBNull(9) == false)
                    departAirport = reader.GetString(9);
                else
                    departAirport = null;
                if (reader.IsDBNull(10) == false)
                    month = reader.GetString(10);
                else
                    month = null;
                if (reader.IsDBNull(11) == false)
                    duration = reader.GetString(11);
                else
                    duration = null;
                if (reader.IsDBNull(12) == false)
                    wasPrice = reader.GetString(12);
                else
                    wasPrice = null;
                if (reader.IsDBNull(13) == false)
                    nowPrice = reader.GetString(13);
                else
                    nowPrice = null;
                if (reader.IsDBNull(14) == false)
                    optStatement = reader.GetString(14);
                else
                    optStatement = null;
                if (reader.IsDBNull(15) == false)
                    offerText = reader.GetString(15);
                else
                    offerText = null;
                if (reader.IsDBNull(16) == false)
                    optText = reader.GetString(16);
                else
                    optText = null;
                if (reader.IsDBNull(17) == false)
                    fullDescription = reader.GetString(17);
                else
                    fullDescription = null;
                if (reader.IsDBNull(18) == false)
                    url = reader.GetString(18);
                else
                    url = null;
                if (reader.IsDBNull(19) == false)
                    ord = reader.GetString(19      );
                else
                    ord = null;

                reader.Close();
            }
            catch
            {

            }
        }
        catch { }

        connection.Close();
    }

    private void Get_Connection()
    {
        connection_open = false;

        connection = new MySqlConnection();
        //connection = DB_Connect.Make_Connnection(ConfigurationManager.ConnectionStrings["SQLConnection"].ConnectionString);
        connection.ConnectionString = ConfigurationManager.ConnectionStrings["MySQLConnection"].ConnectionString;

        // if (db_manage_connnection.DB_Connect.OpenTheConnection(connection))
        if (Open_Local_Connection())
        {
            connection_open = true;
        }
        else
        {
            // MessageBox::Show("No database connection connection made...\n Exiting now", "Database Connection Error");
            // Application::Exit();
        }
    }

    private bool Open_Local_Connection()
    {
        try
        {
            connection.Open();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
}
}

目前,它从检索到的数据库表中保存了一行。请解释如何通过阅读器检索数据库表中的所有行。

1 个答案:

答案 0 :(得分:0)

reader.Read()替换为以下

while(reader.Read()) 
{
  var value1 = reader.GetValue(0); // Replace with your code
  var value2 = reader.GetValue(1); // Replace with your code
  var value3 = reader.GetValue(2); // Replace with your code
}

在旁注中,可以尝试查看automapper之类的内容以映射到您的对象。此外,最好在业务逻辑和数据访问逻辑之间建立一个分离层。

<强> 修改

使用Read对象的DataReader方法从查询结果中获取一行。您可以通过将列的名称或序号引用传递给DataReader来访问返回行的每一列。 因此,因为您循环遍历DataReader每次迭代将获得reader

中的下一行

希望这更有意义。