正在创建sqldatareader类

本文关键字:sqldatareader 创建 | 更新日期: 2023-09-27 18:00:08

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

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);
}

使用列表(它似乎不喜欢"return checkpoint;")公共类检查点{公共字符串检查点1{get;set;}公共字符串检查点2{get;set;}公共字符串检查点3{get;set;}公共字符串检查点4{get;set;}公共字符串检查点5{get;set;}}

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

正在创建sqldatareader类

我为您编写了一个类,该类连接到SQL数据库,并以List<T>的形式返回检查点,其中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中创建一个类库类型的新项目,并将该类复制到类库中