使用sql datareader读取数据 [英] Data reading with sql datareader

查看:103
本文介绍了使用sql datareader读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用数据阅读器从数据库中读取数据。

我有一个文本框和一个单击按钮。

当我在文本框中输入数据并单击搜索然后它应该显示数据。

如何使用Datareader并在gridview中显示?



请帮帮我。 。

I want to read data from database using a data reader.
I have a text box and a click button.
When i enter data in the textbox and click search then it should display the data .
How to do it with Datareader and display the in a gridview?

Please help me out..

推荐答案

最好使用DataAdapter从数据库填充datagridview。

数据读取器主要用于我们必须获取和处理数据的地方一行一行。

ie

Its better to use DataAdapter to fill a datagridview from database .
Data reader used mostly where we have to fetch and process data row one by one.
i.e.
Dim ConnStr As String= "your connection string"
Dim SConn As System.Data.SqlClient.SqlConnection
Dim SqlStat As String="select * from tableName"
SConn = New System.Data.SqlClient.SqlConnection(ConnStr)
SConn.Open()  'open connection



使用DataAdapter非常简单。


Using DataAdapter is so simple.

Dim ds As DataSet = New DataSet()
Dim da As System.Data.SqlClient.SqlDataAdapter
da = New System.Data.SqlClient.SqlDataAdapter(SqlStat, SConn)
da.Fill(ds, "myTable")
DatagridView1.DataSource=ds.tables("myTable") 'We candirectly bind dataset to DataGridView.





如果你必须使用datareader那么

使用datareader:





If you must have to use datareader then
using datareader :

Dim SComm As System.Data.SqlClient.SqlCommand
SComm = New System.Data.SqlClient.SqlCommand(SqlStat, SConn)
Dim reader As SqlDataReader= SComm.ExecuteReader
      'We cannot directly bind DataReader to DataGridView.
Dim table As New DataTable()
table.Load(reader)
DatagridView1.DataSource=table



最后关闭连接


At Last close connection

SConn.Close()'close connection


sqlconnection con=new seqlconnection("Write Connection String")
sqlCommand cmd=new SqlCommand("Select * from table where columnname like '%"+texbox.text+"'",con)
cmd.con.open();
sqldatareader dr=cmd.ExecuteReader();
dr.read();
datatable dt=new datatable();
dt.Load(dr);
cmd.con.close();
GridView.DataSource=dt;
Gridview.DataBind();


这篇关于使用sql datareader读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆