mysql - 需要对代码进行一些修改

标签 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;
        }
    }
}
}

目前它正在从检索到的数据库表中保存一行。请说明如何通过reader检索数据库表中的所有行。

最佳答案

将您的 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 之类的东西来映射到您的对象,此外,在业务逻辑和数据访问逻辑之间建立一个分离层也是一种很好的做法。

编辑

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

中的下一行

希望这更有意义。

关于mysql - 需要对代码进行一些修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30350906/

相关文章:

asp.net-mvc - 为所有客户端创建相同的 SignalR Hub

php - 如何使用Union优化Mysql Select查询?

c# - 寻找 C# 缓存管理器

php - 如果不存在则创建一个 mysql 记录,否则更新它

asp.net - AngularJs + ServiceStack 应用程序的安全性

javascript - 用户登录后如何通过浏览器记住登录详细信息

c# - ASP.NET MVC Razor 语法

c# - 具有派生类的 Controller 操作

mysql - 带有 MySQL 的 Entity Framework 6 - UniqueIdentifier 问题

mysql - Django 2.0 - Python3.6 - CentOS 7 - MySQL 连接