【问题标题】:Creating a sqldatareader class创建一个 sqldatareader 类
【发布时间】:2016-08-03 06:38:48
【问题描述】:

我正在努力创建一个可以从其他类访问的类,这些类可以在我的整个应用程序中访问。我是创建课程的新手,并欣赏任何合乎逻辑的提示。最终,我想将“LUSDschoolDates”中的“checkpoint1-5”值访问为“chk1”、“chk2”等。不确定我是否正确填充数据或更好地使用列表??下划线部分表示我正在挣扎的区域(这基本上是告诉我我无法访问)以及我喜欢它在哪里工作。该项目的目标是创建一个后端管理页面,用户可以在其中插入日期时间数据,并且这些值需要在整个应用程序中都可以访问..

public class checkpoint
{
    public string checkpoint1 { get; set; }
    public string checkpoint2 { get; set; }
    public string checkpoint3 { get; set; }
    public string checkpoint4 { get; set; }
    public string checkpoint5 { get; set; }
}

public class myCheckpoints
{
    public static string Checkpoints()

    {

        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["lusdMembership"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("SELECT chk1, chk2, chk3, chk4, chk5 FROM checkpoints", connection);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                checkpoint c = new checkpoint();
                c.checkpoint1 = reader["chk1"].ToString();
                c.checkpoint2 = reader["chk2"].ToString();
                c.checkpoint3 = reader["chk3"].ToString();
                c.checkpoint4 = reader["chk4"].ToString();
                c.checkpoint5 = reader["chk5"].ToString();  
            }
            return Checkpoints();
        }

    }
}


public class LUSDschoolDates
{

    public static DateTime chk1 = new DateTime(checkpoint.checkpoint1);

}

使用列表(它似乎不像“返回检查点;”) 公共类检查点 { 公共字符串检查点 1 { 获取;放; } 公共字符串检查点2 { 获取;放; } 公共字符串检查点 3 { 获取;放; } 公共字符串 checkpoint4 { 获取;放; } 公共字符串检查点 5 { 获取;放; } }

public class myCheckpoints
{
    public List<checkpoint> GetDate(string chDate)

    {

        List<checkpoint> Checkpoints = new List<checkpoint>();
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["lusdMembership"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("SELECT chk1, chk2, chk3, chk4, chk5 FROM checkpoints", connection);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                checkpoint c = new checkpoint();
                c.checkpoint1 = reader["chk1"].ToString();
                c.checkpoint2 = reader["chk2"].ToString();
                c.checkpoint3 = reader["chk3"].ToString();
                c.checkpoint4 = reader["chk4"].ToString();
                c.checkpoint5 = reader["chk5"].ToString();

            }
            return checkpoint;
        }

    }

【问题讨论】:

  • 您创建了一个新的检查点实例但不返回它。不应该是“返回检查点”吗?而不是“返回 CheckPoints();”?此外,如果您希望从数据库返回多个对象,那么您需要将它们添加到某种集合中(例如 List
  • 我使用列表发布了一个编辑(正如我最初所做的那样),但它不喜欢“返回检查点;”??
  • checkpoint 是类型,而不是列表。您应该返回Checkpoints。但是,您永远不会向列表添加任何检查点,因此列表将为空。这实际上是一个 C# 语法问题,而不是 ASP.NET 或 SqlDataReader 问题。顺便说一句,您可能应该为类型和字段名称使用正确的大小写以避免混淆,例如,Checkpoint 用于类型,checkpoints 用于列表

标签: c# asp.net class sqldatareader


【解决方案1】:

我为您编写了一个连接到 SQL 数据库的类,并以 List&lt;T&gt; 的形式返回检查点,其中 T 是一个名为 CheckPoint 的类。您还缺少打开和关闭我已经建立的 SQL 连接的逻辑包括:

public class CheckPoint
{
    public string CheckPoint1 { get; set; }
    public string CheckPoint2 { get; set; }
    public string CheckPoint3 { get; set; }
    public string CheckPoint4 { get; set; }
    public string CheckPoint5 { get; set; }
}

public class MyCheckpoints
{
    public List<CheckPoint> GetCheckPoins()
    {
        List<CheckPoint> checkpoints = new List<CheckPoint>();

        string connectionString = ConfigurationManager.ConnectionStrings["lusdMembership"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand("SELECT chk1, chk2, chk3, chk4, chk5 FROM checkpoints", connection);
            connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    CheckPoint c = new CheckPoint
                    {
                        CheckPoint1 = reader["chk1"].ToString(),
                        CheckPoint2 = reader["chk2"].ToString(),
                        CheckPoint3 = reader["chk3"].ToString(),
                        CheckPoint4 = reader["chk4"].ToString(),
                        CheckPoint5 = reader["chk5"].ToString(),
                    };
                    checkpoints.Add(c);
                }
                connection.Close();
            }
        }
        return checkpoints;
    }
}

下面是从 ASP.NET 调用它的方法:

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        System.Diagnostics.Debugger.Break();

        MyCheckpoints myCheckpoints = new MyCheckpoints();
        List<CheckPoint> checkPoints = myCheckpoints.GetCheckPoins();
        int count = checkPoints.Count;
    }
}

如果你想让MyCheckpoints 可以在不同的项目中重复使用,你应该在 Visual Studio 中创建一个类库类型的新项目,并将这个类复制到类库中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多