问题描述
新手问题:
我有一些我正在读的数据并把所述数据视图randomg电影资料一个SQLite数据库。然而,现在它正在经历表,并只增加的最后一条记录的视图。如何添加每条记录作为SQLiteDataReader是通过表而不是表中的?
刚刚过去的记录这是我的电流控制器code:
公众的ActionResult dbView()
{
字符串CS =数据源=+ Environment.CurrentDirectory +\\\\ example.db 使用(SQLiteConnection CON =新SQLiteConnection(CS))
{
con.Open(); 字符串STM =SELECT * FROM电影; 使用(SQLiteCommand CMD =新SQLiteCommand(STM,CON))
{
使用(SQLiteDataReader RDR = cmd.ExecuteReader())
{
而(rdr.Read())
{
ViewBag.movie = RDR [MovieId];
ViewBag.title = RDR [标题];
ViewBag.rating = RDR [评级];
ViewBag.image = RDR [形象];
}
}
} VAR图像=数据:图像/ PNG; BASE64,+ Convert.ToBase64String(ViewBag.image);
ViewBag.image =图像;
con.Close();
} 返回查看();
}
我的看法code:
< DIV CLASS =COL-MD-4>
<表样式=宽度:100%BORDER =1>
&所述; TR>
<第i个标题< /第i
<第i个评价< /第i
< / TR>
&所述; TR>
< TD> @ ViewBag.title< / TD>
< TD> @ ViewBag.rating< / TD>
< / TR>
< /表> < IMG SRC =@ ViewBag.image的风格=宽度:300像素/>
< / DIV>
更新:这里就是我在目前是:
公开名单<电影及GT;电影{搞定;组; } 公众的ActionResult dbView()
{
字符串CS =数据源=+ Environment.CurrentDirectory +\\\\ example.db 使用(SQLiteConnection CON =新SQLiteConnection(CS))
{
VAR listOfMovies =新的List<电影及GT;();
con.Open();
字符串STM =SELECT * FROM电影; 使用(SQLiteCommand CMD =新SQLiteCommand(STM,CON))
{
使用(SQLiteDataReader RDR = cmd.ExecuteReader())
{
而(rdr.Read())
{
listOfMovies.Add(新电影
{
MovieID = int.Parse(读卡器[雇员]。的ToString()),
标题=读卡器[标题]的ToString()
等级=读卡器[等级。toString()方法,
});
}
rdr.Close();
电影= listOfMovies;
}
} con.Close();
}
返回查看(电影); //这个写着类名是无效
}
}
和我的模型:
公共类电影
{
公众诠释MovieID {获取;设置;}
公共字符串名称{获取;设置;}
公众诠释评级{获取;设置;}
公共字节图片{获取;设置;}
}
添加到视图:
@foreach(在电影VAR项目)//这个说不能解析符号电影
{
&所述; TR>
< TD> @ ViewBag.title< / TD>
< TD> @ ViewBag.rating< / TD>
< / TR>
}
使用SQL精简版,我还没有经验,但它应该服务于同一目的,MS SQL SERVER
使用我的一些code作为修改你的目的的例子。
公开名单<电影及GT;电影{搞定;组; }公共无效连接()
{
字符串CS =数据源=+ Environment.CurrentDirectory +\\\\ example.db
使用(VAR连接=新SQLiteConnection(CS))
{ VAR listOfMovies =新的List<电影及GT;();
VAR STM =SELECT * FROM电影;
VAR命令=新SQLiteCommand(STM,连接);
尝试
{ 变种读者= Command.ExecuteReader却(); 而(reader.Read())
{ listOfMovies.Add(新电影
{ MovieID = int.Parse(读卡器[雇员]。的ToString()),
标题=读卡器[标题]的ToString()
等级=读卡器[等级。toString()方法,
//你应该用图像做什么都。 });
}
reader.Close();
电影= listOfMovies;
} 赶上(异常前)
{
扔;
} } }公共类电影
{
公众诠释MovieID {获取;设置;}
公共字符串名称{获取;设置;}
公众诠释评级{获取;设置;}
公共字节图片{获取;设置;}
}
只需将电影榜回返回视图(模型),尽量不要使用Viewbag属性这种类型的事情。
和你CSHTML:
< DIV CLASS =COL-MD-4>
<表样式=宽度:100%BORDER =1>
&所述; TR>
<第i个标题< /第i
<第i个评价< /第i
< / TR>
@foreach(在model.Movies VAR项)
{
&所述; TR>
< TD> item.title< / TD>
< TD> item.rating< / TD>
// EDITED HERE FOR IMAGE
//这应该注意,这code是未经考验的。
&所述; TD>&下; IMG SRC =item.imageFilePath宽度=300像素;/>&下; / TD> < / TR>
}< /表>< IMG SRC =@ ViewBag.image的风格=宽度:300像素/>
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. 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?
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
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.
and in your 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"/>
这篇关于从SQLite数据库ASP.NET MVC应用程序读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!