SQL DAtaReader和VB [英] SQL DAtaReader and VB

查看:90
本文介绍了SQL DAtaReader和VB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我如何使用SqlDataReader读取表中的所有列按ID搜索这里是我的代码的一部分



Hi how can i use the SqlDataReader to read all the columns within the table seraching by id here is part of my code

selectCommand.CommandText = "SELECT * FROM tblEjemplo WHERE COOP_ID = @COOP_ID"

    Try
        conn.Open()

    Catch ex As SqlException
        MessageBox.Show(ex.Message)

推荐答案

从这里开始: ADO.NET概述 [ ^ ]然后阅读:构建ADO.NET应用程序 [ ^ ]



其他有用链接: http://msdn.microsoft.com/en-us/ library / dw70f090%28v = vs.71%29.aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue(v = vs.85) .aspx [ ^ ]
Start here: Overview of ADO.NET[^] then read this: Building ADO.NET Applications[^]

Other useful links: http://msdn.microsoft.com/en-us/library/dw70f090%28v=vs.71%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue(v=vs.85).aspx[^]


您好,



您可以尝试以下代码,我使用DataAdapter填充gridview。



1.在web.config文件中定义连接字符串。

Hi,

You can Try below code, where i fill the gridview by using DataAdapter.

1. define connection string in web.config file.
<connectionstrings>
<add name="ConnectionStringName" connectionstring="Data Source=abc;Initial Catalog=xyz;User ID=mno;Password=pqr" />
</connectionstrings>





2.导入一些命名空间。



2. Import some namespace.

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration





3.现在调用函数BindGridView(COOP_ID)来使用DataAdapter填充GridView,其中i我通过COOP_ID = 1你可以根据需要通过。





3. Now call function BindGridView(COOP_ID) to fill GridView by using DataAdapter, where i am passing COOP_ID = 1 you can pass as per your need.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GridView1.DataSource = BindGridView(1)
        GridView1.DataBind()
    End Sub

    Private Function BindGridView(ByVal COOP_ID As Integer) As DataTable
        Dim connstr = ConfigurationManager.ConnectionStrings("ConnectionStringName").ConnectionString
        Dim con As SqlConnection = New SqlConnection(connstr)
        Dim da As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM tblEjemplo WHERE COOP_ID = " + COOP_ID + "", con)
        Dim dt As New DataTable()
        da.Fill(dt)
        Return dt
    End Function


这篇关于SQL DAtaReader和VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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