怎样使用参数查询
如果想通过一张表的某一个字段查询出这张表的全部信息,并用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为接收受此查询影响的行数.
补充:.NET技术 , VB.NET