首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

怎么将sql server中查询记录怎么输出到浏览器中?多谢

2012-02-05 
如何将sql server中查询记录如何输出到浏览器中?谢谢!asp.net+sqlserver如以下代码:ProtectedSubButton1_C

如何将sql server中查询记录如何输出到浏览器中?谢谢!
asp.net   +sql   server
如以下代码:
Protected   Sub   Button1_Click(ByVal   sender   As   Object,   ByVal   e   As   System.EventArgs)   Handles   Button1.Click
                Dim   oCmd   As   Data.SqlClient.SqlCommand
                Dim   strConn   As   String

                strConn   &=   "Data   source=(local); "
                strConn   &=   "initial   catalog=gzgl; "
                strConn   &=   "user   id=sa; "
                strConn   &=   "password=lifan; "

                oCmd   =   New   Data.SqlClient.SqlCommand
                oCmd.Connection   =   New   Data.SqlClient.SqlConnection(strConn)
                oCmd.Connection.Open()
                oCmd.CommandText   =   "select   *   from   jizhan   where   基站名= 'aaa ' "
               
                oCmd.Connection.Close()
        End   Sub
问题:对sql   server数据库进行查询后,查询结果如何输出到浏览器中?
查了一下资料,似乎要用datareaders对象,但不知具体用法,比如如下代码:
Dim   myreader   As   SqldataReader=Nothing
myreader=myCommand.ExecuteReader()
While   myreader.Read
    szname=myreader( "name ").ToString
    szemail=myReader( "email ").Tostring()
end   while
不清楚上面代码的作用
如果将SQL   server   查询的多条记录输出到浏览器,该如何操作?
感激不尽!


[解决办法]
使用SqlDataReader对象时,一次只能读一条记录,并且只能向前读。
对于只读一条记录时,可以使用SqlDataReader对象。若多条,则不方便。
你可以使用一个数据控件,如DataList,GridView等。方便快捷。
将查询结果生成一个DataView,或者DataSet.
如:
SqlConnection conn=new SqlConnection( "server=.;database=gzgl;uid=sa;pwd=lifan; ");
conn.Open();
SqlCommand cmd=new SqlCommand( "select * from jizhan where 基站名= 'aaa ' ",conn);
SqlDataAdepter sda=new SqlDataAdepter(cmd);
DataSet ds=new DataSet();
sda.Fill(ds, "ds ");
GridView.DataSource=sda.tables[ "ds "].DefaultView;
GridView.DataBind();
使用数据源绑定数据控件,查询的数据记录就可以显示在浏览器了。

[解决办法]
Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oCmd As Data.SqlClient.SqlCommand
Dim strConn As String

strConn = "Data source=.; "
strConn &= "initial catalog=Northwind; "
strConn &= "user id=sa; "
strConn &= "password=password "

oCmd = New Data.SqlClient.SqlCommand
oCmd.Connection = New Data.SqlClient.SqlConnection(strConn)
oCmd.CommandText = "select * from Customers "
oCmd.Connection.Open()

Dim myreader As Data.SqlClient.SqlDataReader = Nothing
myreader = oCmd.ExecuteReader()
While myreader.Read


Response.Write(myreader( "CustomerID ").ToString() & " " & myreader( "CompanyName ").ToString() & " <br> ")
End While

myreader.Close()
oCmd.Connection.Close()
End Sub
[解决办法]
如果绑定到控件上
//aspx
<form id= "form1 " runat= "server ">
<div>
<asp:Button ID= "Button1 " runat= "server " Text= "Button " />
<asp:GridView ID= "GridView1 " runat= "server ">
</asp:GridView>
</div>
</form>

//aspx.cs
Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oCmd As Data.SqlClient.SqlCommand
Dim strConn As String

strConn = "Data source=.; "
strConn &= "initial catalog=Northwind; "
strConn &= "user id=sa; "
strConn &= "password=password "

oCmd = New Data.SqlClient.SqlCommand
oCmd.Connection = New Data.SqlClient.SqlConnection(strConn)
oCmd.CommandText = "select * from Customers "
oCmd.Connection.Open()

Dim myreader As Data.SqlClient.SqlDataReader = Nothing
myreader = oCmd.ExecuteReader()
GridView1.DataSource = myreader
GridView1.DataBind()
myreader.Close()
oCmd.Connection.Close()
End Sub

热点排行