创建sqldatareader类

时间:2016-08-03 06:38:48

标签: c# asp.net class sqldatareader

我正在努力创建一个可以从我的应用程序中可访问的其他类访问的类。我是创建课程的新手,并欣赏任何合理的提示。最后,我想在“LUSDschoolDates”中将“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);

}

使用列表(它似乎不喜欢“返回检查点;”)      公共阶层检查站     {         public string checkpoint1 {get;组; }         public string checkpoint2 {get;组; }         public string checkpoint3 {get;组; }         public string checkpoint4 {get;组; }         public string checkpoint5 {get;组; }     }

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

    }

1 个答案:

答案 0 :(得分:0)

我为你编写了一个连接到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类型库中创建一个新项目,并将该类复制到类库中。