从数据库表中选择信息 [英] Select information from database table
问题描述
您好,我正在使用ADO .NET数据实体模型进行ac#项目,我有一个表主题列(idSubject(int),nameSubject(nvarchar),subjectNoted(bit / boolean)。我来自表格第一个主题有`subjectNoted == false`然后我点击一个按钮后我将值改为`== true`:
私有 void button1_Click( object sender,EventArgs e)
{
使用(DbEntities db = new DbEntities())
{
主题firstSubject = db.Subjects.FirstOrDefault(s = > s.subjectNoted == false );
if (firstSubject!= null )
{
firstSubject.subjectNoted = true ;
db.SaveChanges();
MessageBox.Show( 主题注释, 确定);
}
}
}
private void button2_Click( object sender,EventArgs e)
{
// 显示下一个主题的代码:subjectNoted == false
}
我有另一个按钮,这个必须忽略第一个主题,并用`subjectNoted == false`显示第二个主题。我创建了一个方法ShowSubject,但我找不到如何选择第二个主题有`subjectNoted == false`并用方法显示它?
private void ShowSubject(主题主题)
{
using (DbEntities db = new DbEntities())
{
Subjects firstSubject = db.Subjects.FirstOrDefault (s = > s.subjectNoted == false );
if (firstSubject!= null )
{
textBox1。 Text = firstSubject.nameSubject;
}
}
}
有人可以帮助我,我会被困在这里!
您是否尝试过使用Skip()和Take()方法?
Skip(n)将跳转n条记录和Take(n)将返回许多记录。
例如你可以尝试
var subject = db
.Subjects
.Where(s = > s.subjectNoted = = false )
.OrderBy(s = > s.idSubject)
.Skip( 1 )
.Take( 1 )
.FirstOrDefault() ;
有一件事是你需要在查询之前添加订单才能跳过/取走。
private void ShowSubject(Subjects subject)
{
使用(DbEntities db = new DbEntities())
{
主题firstSubject = db.Subjects.FirstOrDefault(s => s.subjectNoted == false).Skip(1).Take(1).FirstOrDefault();
if(firstSubject!= null)
{
textBox1.Text = firstSubject.nameSubject;
}
}
}
Hello, i m doing a c# project with ADO .NET data entity model, i have a table Subjects with columns (idSubject(int), nameSubject(nvarchar), subjectNoted(bit/boolean). I''m getting from the table the first subject that have `subjectNoted==false` then i change the value to `==true` after clickin a button with this code:
private void button1_Click(object sender, EventArgs e)
{
using (DbEntities db = new DbEntities())
{
Subjects firstSubject = db.Subjects.FirstOrDefault(s => s.subjectNoted == false);
if (firstSubject != null)
{
firstSubject.subjectNoted = true;
db.SaveChanges();
MessageBox.Show("Subject Noted", "OK");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
//code to show the next subject that have subjectNoted==false
}
I have another button, this one must ignore the first subject and show the second one with `subjectNoted==false`. I created a method ShowSubject, but i can''t find how to select the second subject that have `subjectNoted==false` and show it with the methode??
private void ShowSubject(Subjects subject)
{
using (DbEntities db = new DbEntities())
{
Subjects firstSubject = db.Subjects.FirstOrDefault(s => s.subjectNoted == false);
if (firstSubject != null)
{
textBox1.Text = firstSubject.nameSubject;
}
}
}
Someone can help me please i''m stuck here!
Have you tried using Skip() and Take() methods ?
Skip(n) will jump the n number of records and Take(n) will return n many records.
for example you could try
var subjects = db .Subjects .Where(s=> s.subjectNoted == false) .OrderBy(s=> s.idSubject) .Skip(1) .Take(1) .FirstOrDefault();
One thing is you do need to add an order by to your query before you can do a skip / take.
private void ShowSubject(Subjects subject)
{
using (DbEntities db = new DbEntities())
{
Subjects firstSubject = db.Subjects.FirstOrDefault(s => s.subjectNoted == false).Skip(1).Take(1) .FirstOrDefault();
if (firstSubject != null)
{
textBox1.Text = firstSubject.nameSubject;
}
}
}
这篇关于从数据库表中选择信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!