从数据库中获取gridview中的数据... [英] get data in gridview from database...
问题描述
我有一个salary_master表。我想在使用数据集加载表单时从数据库中将这些数据提取到datagrid-view中。如何从数据集中仅获取所需数据(即其中一些但不是全部),并保留gridview的头文件。定义为它也从数据库加载列。下面是将数据提取到gridiew中的代码。还有哪个更好:数据集或使用数据阅读器
------------------------------- --------------------------------------
代码:
-------------------------------------------- -------------------------
私人 Sub Load_Data_Grid()
尝试
con.Open()
ss = SELECT * from salary_master
com = 新 SqlCommand(ss,con)
da = 新 SqlDataAdapter(com)
da.Fill(ds, salary_master)
DGV_Salary.DataSource = ds。表( 0 )
com.ExecuteNonQuery()
Catch ex As 异常
MsgBox(ex.Message)
最后
con.Close()
结束 尝试
结束 Sub
---- -------------------------------------------------- --------------
和在Load事件中我调用了上面的程序...
< blockquote>你可以创建限制返回记录数的sql参数,例如:
Dim con As New SqlConnection(sqlConnectionString)
试试
con.Open()
Dim ss As String =SELECT * from myTable where id = @ id
Dim com As New SqlCommand(ss,con)
Dim da As New SqlDataAdapter(com )
Dim param As New SqlParameter
param.SqlDbType = SqlDbType.Int
param.ParameterName =@ id
param.Value = 7200
da.SelectCommand.Parameters.Add(param)
Dim ds As New DataSet
da.Fill(ds,my_table)
dg.DataSource = ds.Tables(0)
com.ExecuteNonQuery()
C ex ex As Exception
MsgBox(ex.Message)
最后
con.Close()
结束尝试
在此查看更多信息:
https://msdn.microsoft.com/en-us/library/ms254953(v = vs.110).aspx [ ^ ]
I have a salary_master table. And I want to fetch those data from database into the datagrid-view when the form loads using dataset.So, how to fetch only required data (i.e some of them but not all) from dataset and also keep the headers of gridview that I have defined as it loads the the columns also from database. below is the code for fetching data into the gridiew. Also which is better : dataset or using data-reader
---------------------------------------------------------------------
code:
---------------------------------------------------------------------
Private Sub Load_Data_Grid()
Try
con.Open()
ss = "SELECT * from salary_master"
com = New SqlCommand(ss, con)
da = New SqlDataAdapter(com)
da.Fill(ds, "salary_master")
DGV_Salary.DataSource = ds.Tables(0)
com.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
End Try
End Sub
--------------------------------------------------------------------
and in Load event I have called the above procedure...
You can create sql parameters that would limit the number of records returned, for example:
Dim con As New SqlConnection(sqlConnectionString) Try con.Open() Dim ss As String = "SELECT * from myTable where id=@id" Dim com As New SqlCommand(ss, con) Dim da As New SqlDataAdapter(com) Dim param As New SqlParameter param.SqlDbType = SqlDbType.Int param.ParameterName = "@id" param.Value = 7200 da.SelectCommand.Parameters.Add(param) Dim ds As New DataSet da.Fill(ds, "my_table") dg.DataSource = ds.Tables(0) com.ExecuteNonQuery() Catch ex As Exception MsgBox(ex.Message) Finally con.Close() End Try
Look here for further info:
https://msdn.microsoft.com/en-us/library/ms254953(v=vs.110).aspx[^]
这篇关于从数据库中获取gridview中的数据...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!