OleDbConnection con;
OleDbDataReader read;
OleDbCommand cmd;
private void btn_clicked(Object sender, EventArgs e)
{
con = new OleDbConnection(WindowsFormsApplication2.Properties.Settings.Default.DBConString);
con.Open();
cmd = new OleDbCommand( " SELECT * FROM tbPeople WHERE country = ' " textbox.Text " ' ", con );
read = cmd.ExecuteReader();
while(read.Read())
{
textboxTest.text += read["lastname"].toString() + ", " + read["firstname"].toString() + "\n";
}
}
我是Entity Framework的新手,我希望转换此代码。
提前致谢:)
答案 0 :(得分:1)
假设您创建实体时不使用tb作为前缀并使用普通套管
using(var db = new MyAwesomeContext())
{
var names = db.People.Where(x => x.Country == textbox.Text)
.Select(x => $"{x.LastName}, {x.FirstName}")
.ToList();
textboxTest.text = string.Join("\n", names);
}
其他资源
Enumerable.Where Method (IEnumerable, Func)
根据谓词过滤一系列值。
Enumerable.Select Method (IEnumerable, Func)
将序列的每个元素投影到新表单中。
String.Join Method (String, String[])
使用指定的连接字符串数组的所有元素 每个元素之间的分隔符。
答案 1 :(得分:0)
假设您已经创建了edmx数据模型并且数据上下文为dbContext
,那么您的等效查询将有点像:
var country=textbox.text;
var peoples=dbContext.tbPeople.where(c=>c.country==country).ToList()
foreach(var people in peoples)
{
textboxTest.text += people.lastname + ", " + people.firstname + "\n";
}