使用linq在datagrid中获取数据... [英] Getting data in datagrid using linq...
问题描述
我有一个datagrid,当它加载时,所有记录都从数据库中获取并显示在datagrid中。在表单中,我还具有通过id搜索记录的功能。但我希望通过使用LINQ来完成它,即它应该从datagrid搜索并显示我输入的id的记录。下面是使用datareader通过id获取记录的代码。
----------------------------- ------------------------------------
代码:
---------------------------------------------- -------------------
私人 Sub btnsearch_Click(发件人作为 对象,e As EventArgs)句柄 btnsearch.Click
DGV_empno.Rows.Clear()
尝试
con.Open()
com = 新 SqlCommand( SELECT * from emp_master where empid = @ empid,con)
com.Parameters.AddWithValue(< span class =code-string> @ empid,txtempno.Text)
dr = com.ExecuteReader
while dr.Read
i = DGV_empno.Rows.Add(+1)
DGV_empno.Rows(i )。细胞( 0 )。值= dr( empid)
DGV_empno.Rows(i).Cells( 1 )。值= dr( empname)
DGV_empno.Rows(i).Cells( 2 )。Value = dr( dob)
DGV_empno.Rows(i)。单元格( 3 )。值= dr( doj )
DGV_empno.Rows(i).Cells( 4 )。值= dr( dept)
DGV_empno.Rows(i)。单元格( 5 )。值= dr( desig )
DGV_empno.Rows(i).Cells( 6 )。值= dr( exp)
DGV_empno.Rows(i).Cells( 7 ) .Value = dr( salary)
结束
Catch ex As 异常
MsgBox(ex.Message)
最后
dr.Close()
con.Close()
结束 尝试
结束 Sub
我不知道是什么阻止你使用Linq;)
看看这里:
Visual Basic中的LINQ简介 [ ^ ]
VB.NET中的LINQ to SQL [ ^ ]
在Visual Basic中使用LINQ to SQL [ ^ ]
查询数据库se使用Visual Basic 2010进行LINQ to SQL [ ^ ]
LINQ - 样本查询(101个样本) [ ^ ]
我希望这组文章可以帮助你编写正确的代码; )
I have a datagrid and when it loads all the records are fetched from database and displayed in the datagrid. In the form I also have functionality to search the record by id. but I want to get it done by using LINQ i.e it should search from datagrid and display the record of that id which I enter. Below is the code for fetching the record by id using datareader.
-----------------------------------------------------------------
code:
-----------------------------------------------------------------
Private Sub btnsearch_Click(sender As Object, e As EventArgs) Handles btnsearch.Click
DGV_empno.Rows.Clear()
Try
con.Open()
com = New SqlCommand("SELECT * from emp_master where empid=@empid", con)
com.Parameters.AddWithValue("@empid", txtempno.Text)
dr = com.ExecuteReader
While dr.Read
i = DGV_empno.Rows.Add(+1)
DGV_empno.Rows(i).Cells(0).Value = dr("empid")
DGV_empno.Rows(i).Cells(1).Value = dr("empname")
DGV_empno.Rows(i).Cells(2).Value = dr("dob")
DGV_empno.Rows(i).Cells(3).Value = dr("doj")
DGV_empno.Rows(i).Cells(4).Value = dr("dept")
DGV_empno.Rows(i).Cells(5).Value = dr("desig")
DGV_empno.Rows(i).Cells(6).Value = dr("exp")
DGV_empno.Rows(i).Cells(7).Value = dr("salary")
End While
Catch ex As Exception
MsgBox(ex.Message)
Finally
dr.Close()
con.Close()
End Try
End Sub
I have no idea what stops you on using Linq ;)
Have a look here:
Introduction to LINQ in Visual Basic[^]
LINQ to SQL in VB.NET[^]
Using LINQ to SQL in Visual Basic[^]
Querying a Database with LINQ to SQL Using Visual Basic 2010[^]
LINQ - Sample Queries (101 samples)[^]
I hope this set of articles helps you to write proper code ;)
这篇关于使用linq在datagrid中获取数据...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!