[VB.NET]怎样使用参数查询

怎样使用参数查询 如果想通过一张表的某一个字段查询出这张表的全部信息,并用datagrid显示出来,应该怎么做?有哪位高人能提供一段源代码例子给偶啊(偶的抽象思维很弱,一般要看到实例才理解得比较快,呵呵) __________________________________________________________________________ “通过一张表的某一个字段查询出这张表的全部信息”是什么意思? __________________________________________________________________________ 写个存储过程不就行了 imports system.data.sqlclient dim cnn as new sqlconnection dim dataset as new dataset cnn.connectionstring= "server=srvname;database=dbname;uid=sa;pwd= " cnn.open dim adapter as sqldataadapter=new (spname,cnn) dim pr as new sqlparameter pr.name= ''@item '' pr.dbtype=system.type.gettype( "System.string ") pr.type=input adapter.parameters.add(pr) adapter.fill(dataset) datagrid1.datasource=dataset.table(0) __________________________________________________________________________ 楼主问题更抽象哦 看不懂啊。 是用一个字段还是用多个字段? __________________________________________________________________________ 说不清,何以道明? __________________________________________________________________________ 应该就是加条件吧,条件是有一个字段限定的。 那存储过程怎么加到代码里呢? __________________________________________________________________________ ado.net + sql 应该就是这样的: Imports System.Data Imports System.Data.OleDb Public Class Form1 Dim conn As New OleDbConnection Dim connstr,sql As String Dim cmd As OleDbCommand Dim da As New OleDbDataAdapter Dim tbl As New DataTable Private Sub Conn_DB() connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Application.StartupPath & "/123.mdb " conn.ConnectionString = connstr Try conn.Open() Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub DoQuery() Dim Insql As String If conn.State = ConnectionState.Closed Then Conn_DB() sql = "select * from mydata where " Insql = "f1 like ''% " & TextBox1.Text & "% '' " sql &= Insql Insql = "and f2 like ''% " & TextBox2.Text & "% '' " sql &= Insql Insql = "and f3 like ''% " & TextBox3.Text & "% '' " sql &= Insql cmd = New OleDbCommand(sql,conn) da.SelectCommand = cmd tbl.Clear() da.Fill(tbl) DataGridView1.DataSource = tbl End Sub Private Sub Form1_FormClosing(ByVal sender As Object,ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing conn.Close() conn.Dispose() End Sub Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load DoQuery() End Sub Private Sub TextBox1_KeyPress(ByVal sender As Object,ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If Convert.ToInt32(e.KeyChar) = 13 Then TextBox2.Focus() TextBox2.SelectAll() End If End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TextBox1.TextChanged DoQuery() End Sub Private Sub TextBox2_KeyPress(ByVal sender As Object,ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress If Convert.ToInt32(e.KeyChar) = 13 Then TextBox3.Focus() TextBox3.SelectAll() End If End Sub Private Sub TextBox2_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TextBox2.TextChanged DoQuery() End Sub Private Sub TextBox3_KeyPress(ByVal sender As Object,ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress If Convert.ToInt32(e.KeyChar) = 13 Then TextBox1.Focus() TextBox1.SelectAll() End If End Sub Private Sub TextBox3_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TextBox3.TextChanged DoQuery() End Sub End Class 简单的做了个多字段的查询显示控件使用datagridview,你看看吧 __________________________________________________________________________ 在前台程序中,使用参数化查询,如: strMsg = "EXEC sp_executesql " _ & "N''SELECT UserCode,UserName,[Password]库FROM RB_Users WHERE UserCode=@P1''," _ & "N''@P1 NVARCHAR(30)'',@P1=''" & sLoginName & "''" Con.Execute strMsg,lAffected !Con为已打开的连接,lAffected为接收受此查询影响的行数. __________________________________________________________________________

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强制返回为文本 --------------------------...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办法, Format 或者FormatDateTime 竟然结果和...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace My ‘全局错误处理,新的解决方案直接...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用的爽呀,这篇文章写与2011年,看来我以前没...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选中的单元格进行处理 Dim m As Range, t...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integ...