从数据库到填充列表的对象 [英] Objects from database to fill list

查看:24
本文介绍了从数据库到填充列表的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个学校项目,我们正在预订电影.该程序的标准之一是将数据库中的所有电影以列表形式显示.

I'm doing a school project where we are making a cinema booking. One of the criteria of the program is to display all the movies in a database in a list.

我知道这可能是一个非常简单的问题,但我需要一个指向正确方向的指针.

I know this might be a really simple question, but I need a pointer in the right direction.

如何制作包含电影所有数据的电影对象(movieIDmovieNamemovieLengthmovieDesc)

How do I make an object of a movie which contains all data of the movie (movieID, movieName, movieLength, movieDesc)

如何使用这些数据填充列表?

How do I use this data to fill a list?

我们正在使用 C# 中的 WCF 在基于客户端/服务器的架构中编写程序

We are writing the program in a client/server based architecture using WCF in C#

如果您需要进一步的阐述或细节,请告诉我.

Please tell me if you need further elaboration or detail.

提前致谢.这是我在数据库中创建电影的代码,我不确定如何从同一数据库创建电影对象以在我的代码中使用.

Thanks in advance. Here is my code for creating the movies in the database, I am unsure how to create a movie object from same database, to be used in my code.

SqlConnection sc = new SqlConnection();
sc.ConnectionString = ("Data Source=balder.ucn.dk;Initial Catalog=dmaa0213_6;********");
SqlCommand com = new SqlCommand();
sc.Open();
com.Connection = sc;
com.CommandText = ("INSERT into movies (movieName, movieLength, movieDescription) VALUES  ('" + movieName + "','" + movieLength + "','" + movieDesc + "');");
com.ExecuteNonQuery();
sc.Close();

推荐答案

您可以选择使用像 NHibernation 这样的休眠层或来自 Microsoft 的更好的 EntityFramework,或者,您必须手动完成.如果是这样,你必须像这样创建一个 Movie 对象:

You have the choice between using an hibernation layer like NHibernation or the bettwer EntityFramework from Microsoft, or, you have to do it manually. If this is the way, you have to create a Movie object like this:

public class Movie
{
    public string MovieID { get; set; }
    public string MovieName { get; set; }
    public string MovieLength { get; set; }
    public string MovieDesc { get; set; }
}

然后可以将数据库查询读入阅读器并将对象填充到列表中,如下所示:

Then you can read the database query into a reader and fill the objects into a list, like this:

List<Movie> listOfMovies = new List<Movie>();

using(SqlConnection connection = new SqlConnection("Data Source=balder.ucn.dk;Initial Catalog=dmaa0213_6;********"))
{
    using(SqlCommand cmd = new SqlCommand(connection))
    {
        cmd.CommandString = "SELECT * FROM movies ORDER BY MovieId";
        connection.Open();
        using(SqlDataReader reader = cmd.ExecuteDataReader())
        {
            while(reader.Read())
            {
                Movie item = new Movie();
                item.MovieId = reader.GetInt32(0);
                item.MovieName = reader.GetString(1);
                item.MovieLength = reader.GetString(2);
                item.MovieDesc = reader.GetString(3);
                listOfMovies.Add(item);
            }
        }
        connection.Close();
    }
}

这篇关于从数据库到填充列表的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆