简体   繁体   English

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

[英]ASP.NET MVC application reading data from SQLite database

Novice question: 新手问题:

I have an SQLite db with some randomg movie info that I'm reading data from and adding said data to a view. 我有一个带有一些随机电影信息的SQLite数据库,我正在读取数据并将所述数据添加到视图中。 However, right now it's going through the table and only adding the last record to the view. 但是,现在它正在通过表格,只将最后一条记录添加到视图中。 How do I add each record as the SQLiteDataReader is through the table instead of just the last record in the table? 如何通过表格而不是表格中的最后一条记录来添加每条记录,因为SQLiteDataReader是通过表格而不是表格中的最后一条记录?

This is my current controller code: 这是我目前的控制器代码:

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

My view code: 我的观点代码:

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

UPDATE: Here's where I'm currently at: 更新:这是我目前在的地方:

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

And my model: 我的模特:

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

Added to the view: 添加到视图中:

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

I have not had experience using SQL lite but it should serve the same purpose as MS SQl Server 我没有使用SQL lite的经验,但它应该与MS SQl Server具有相同的用途

Using some of my code as an example modified for your purpose. 使用我的一些代码作为修改的示例。

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

Simply return the 'Movies List' back to the View ( In a Model ) try not to use Viewbag properties for this type of thing. 只需将“电影列表”返回到视图(在模型中),尽量不要将Viewbag属性用于此类事物。

and in your cshtml : 并在您的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"/>

Like TinMan7757 wrote but using dataset adapter is better approach: 像TinMan7757写的但是使用数据集适配器是更好的方法:

 using (var sqlDataAdapter = new SqlDataAdapter(sqlCommand)){
    using (var dataSet = new DataSet())
    {
      sqlDataAdapter.Fill(dataSet);

      return dataSet.Tables[0].AsEnumerable().Select(row => new Movie
      {
        MovieID= row["MovieID"].ToString(),
        Title= row["Title"].ToString(),
        Rating= row["Rating"].ToString()
      });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM