c# - ASP.NET MVC 应用程序从 SQLite 数据库读取数据

标签 c# asp.net asp.net-mvc sqlite

新手问题:

我有一个 SQLite 数据库,其中包含一些随机电影信息,我正在从中读取数据并将所述数据添加到 View 中。但是,现在它正在遍历表并仅将最后一条记录添加到 View 中。当 SQLiteDataReader 通过表而不是仅表中的最后一条记录时,如何添加每条记录?

这是我当前的 Controller 代码:

public ActionResult dbView()
    {
        string cs = "Data Source=" + Environment.CurrentDirectory + "\\example.db";

        using (SQLiteConnection con = new SQLiteConnection(cs))
        {
            con.Open();

            string stm = "SELECT * FROM Movie";

            using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
            {
                using (SQLiteDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        ViewBag.movie = rdr["MovieId"];
                        ViewBag.title = rdr["title"];
                        ViewBag.rating = rdr["rating"];
                        ViewBag.image = rdr["image"];
                    }
                }
            }

            var image = "data:image/png;base64," + Convert.ToBase64String(ViewBag.image);
            ViewBag.image = image;


            con.Close();
        }



        return View();
    }

我的查看代码:

<div class="col-md-4">
<table style="width: 100%" border="1">
    <tr>
        <th>Title</th>
        <th>Rating</th>
    </tr>
    <tr>
        <td>@ViewBag.title</td>
        <td>@ViewBag.rating</td>
    </tr>
</table>

    <img src="@ViewBag.image" style="width: 300px"/>
</div>

更新:这是我目前所在的位置:

    public List<Movie> Movies { get; set; }

    public ActionResult dbView()
    {
        string cs = "Data Source=" + Environment.CurrentDirectory + "\\example.db";

        using (SQLiteConnection con = new SQLiteConnection(cs))
        {
            var listOfMovies = new List<Movie>();
            con.Open();
            string stm = "SELECT * FROM Movie";

            using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
            {
                using (SQLiteDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        listOfMovies.Add(new Movies
                        {
                        MovieID= int.Parse(reader["EmployeeId"].ToString()),
                        Title= reader["Title"].ToString(),
                        Rating= reader["Rating"].ToString(),
                        });
                    }
                    rdr.Close();
                    Movies = listOfMovies;
                }
            }

            con.Close();
        }
        return View(Movies); // this says "Class name is not valid"
    }
}

还有我的模型:

public class Movie
{
Public int MovieID {get;set;}
Public String Title {get;set;}
Public int rating {get;set;}
Public Byte Image {get;set;}
}

添加到 View :

    @foreach (var item in Movies) // this says "cannot resolve symbol Movies"
    {
        <tr>
            <td>@ViewBag.title</td>
            <td>@ViewBag.rating</td>
        </tr>
    }

最佳答案

我没有使用 SQL lite 的经验,但它应该与 MS SQl Server 具有相同的用途

使用我的一些代码作为示例,并根据您的目的进行修改。

public List<Movie> Movies{ get; set; }

public void Connect()
        {
         string cs = "Data Source=" + Environment.CurrentDirectory + "\\example.db";
            using (var connection = new SQLiteConnection(cs))
            {

                var listOfMovies= new List<Movie>();
                var stm = "SELECT * FROM Movie";
                var command = new SQLiteCommand(stm , connection);
                try
                {

                    var reader = command.ExecuteReader();

                    while (reader.Read())
                    {

                        listOfMovies.Add(new Movie
                        {

                            MovieID= int.Parse(reader["EmployeeId"].ToString()),
                            Title= reader["Title"].ToString(),
                            Rating= reader["Rating"].ToString(),
                            //Do what ever you should be doing with the image.

                        });
                    }                      
                    reader.Close();                  
                    Movies = listOfMovies;
                }

                catch (Exception ex)
                {
                    throw;
                }

            }

        }

public class Movie
{
Public int MovieID {get;set;}
Public String Title {get;set;}
Public int rating {get;set;}
Public Byte Image {get;set;}
}

只需将“电影列表”返回到 View (在模型中),尽量不要对此类事情使用 Viewbag 属性。

在你的cshtml中:

<div class="col-md-4">
<table style="width: 100%" border="1">
    <tr>
        <th>Title</th>
        <th>Rating</th>
    </tr>
@foreach(var item in model.Movies)
{
<tr>
        <td>item.title</td>
        <td>item.rating</td>
 //EDITED HERE FOR IMAGE
//This should work note that this code is untested.
 <td><img src="item.imageFilePath" width="300px;"/></td>

    </tr>
}

</table>

<img src="@ViewBag.image" style="width: 300px"/>

关于c# - ASP.NET MVC 应用程序从 SQLite 数据库读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32329368/

相关文章:

c# - 等待页面的缓冲区闩锁类型 2 时如何修复 SqlException 超时 (1 :37660679), 数据库 ID 10

asp.net - 哪个更快 : Entity Framework vs stored procedures?

c# - 使用计时器每小时运行一次 if 语句

javascript - Web 方法未被调用

asp.net - 如何在 OnActionExecuting 中获取当前路线信息

c# - asp.net 扩展 IPrincipal

c# - 在 Windows 8 中检查互联网连接(可用性)

c# - 创建带有更新下载进度条的 ListView Windows 8 C# XAML

asp.net-mvc - 仅对登录用户显示菜单项

asp.net - IIS 错误 403.14 - 禁止访问