Dim blUser As Boolean
為西豐等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及西豐網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、西豐網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
Dim sql_conn As New SqlClient.SqlConnection("data source=.;initial catalog=test;user id=sa;password=123456;")
sql_conn.Open()
Dim sql_comm As New SqlClient.SqlCommand("Select user from table where user='123'", sql_conn)
Dim reader As SqlClient.SqlDataReader
reader = sql_comm.ExecuteReader
reader.Read()
If reader.HasRows Then
blUser = True
End If
reader.Close()
sql_conn.Close()
Dim DbCmd as New oledbCommand
DbCmd.CommandType = CommandType.Text
DbCmd.CommandText = CommandText
dim rowsCount as integer = cint(val(DbCmd.ExecuteScalar(select count(*) from table) ""))
直接打開EXCEL用CTRL+F ,可以直接定位到符合條件的數(shù)據(jù);或用指定條件篩選
select * from 表 where
(case when 條件 then 1 else 0 end+
case when 條件 then 1 else 0 end+
case when 條件 then 1 else 0 end+
case when 條件 then 1 else 0 end+
case when 條件 then 1 else 0 end) BETWEEN 2 and 5
以下是完整模塊
Imports
System.Data
Imports
System.IO
Imports
System.Data.OleDb
Module
Module1
Public
cn
As
New
OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source="
Application.StartupPath
"\mdb數(shù)據(jù)庫名字.mdb")
'定義連接
Public
DataBaseRST
As
Integer
'用來返回?cái)?shù)據(jù)庫執(zhí)行結(jié)果
Public
Function
DataModify(ByVal
str
As
String)
As
Boolean
'進(jìn)行數(shù)據(jù)庫修改操作
Dim
cmdinsert
As
New
OleDbCommand
Try
cmdinsert.CommandText
=
str
cmdinsert.Connection
=
cn
If
cn.State
=
ConnectionState.Closed
Then
cn.Open()
DataBaseRST
=
cmdinsert.ExecuteNonQuery()
'用來返回執(zhí)行的結(jié)果
cn.Close()
Return
True
Catch
ex
As
Exception
MessageBox.Show(Err.Description,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error)
Return
False
End
Try
End
Function
Public
Function
Search(ByVal
str
As
String,
ByVal
DGV
As
DataGridView)
As
Boolean
'查詢
str---查詢命令,DGV---DataGridView,用來顯示數(shù)據(jù)的控件
Dim
tb
As
New
DataTable
Try
Dim
ap
As
New
OleDb.OleDbDataAdapter(str,
cn)
ap.Fill(tb)
DGV.DataSource
=
tb
Return
True
Catch
ex
As
Exception
MessageBox.Show(Err.Description,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error)
Return
False
End
Try
End
Function
End
Module
'以下是調(diào)用方法
DataModify("
insert
into
aa
values
('1','2')")'-------這里是數(shù)據(jù)庫更新操作
Search("select
bb
from
aa",DataGridView1)'-----------這里是數(shù)據(jù)表查詢操作
1.新建一張表格,在表格中導(dǎo)入學(xué)生的成績表,如圖所示,將建立一個(gè)按鈕,通過它來實(shí)現(xiàn)查找。
2.單擊菜單欄中的“開發(fā)工具”——插入——表單控件——按鈕,在出現(xiàn)的十字箭頭上拖住畫出一個(gè)按鈕,如圖所示。
3.在彈出的查找紅對話框中選擇“錄制”,在彈出的“錄制新宏”對話框中,修改宏名稱為“查找”,單擊確定。
4. 單擊“開發(fā)工具”——查看代碼,打開VBA編輯器,如圖所示。
5.現(xiàn)在輸入代碼:
Sub 查找()
Dim jieguo As String, p As String, q As String
Dim c As Range
jieguo = Application.InputBox(prompt:="請輸入要查找的值:", Title:="查找", Type:=2)
If jieguo = "False" Or jieguo = "" Then Exit Sub
Application.ScreenUpdating = False
Application.DisplayAlerts = False
With ActiveSheet.Cells
? Set c = .Find(jieguo, , , xlWhole, xlByColumns, xlNext, False)
? If Not c Is Nothing Then
? ? ? p = c.Address
? ? ? Do
? ? ? ? ? c.Interior.ColorIndex = 4
? ? ? ? ? q = q c.Address vbCrLf
? ? ? ? ? Set c = .FindNext(c)
? ? ? Loop While Not c Is Nothing And c.Address p
? End If
End With
MsgBox "查找數(shù)據(jù)在以下單元格中:" vbCrLf vbCrLf _
? q, vbInformation + vbOKOnly, "查找結(jié)果"
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
6.現(xiàn)在回到EXCEL表格,右擊按鈕,選擇“編輯文字”,修改按鈕名稱為“查找按鈕”。
7. 現(xiàn)在單擊查找按鈕,出現(xiàn)對話框“請輸入要查找的值”,在方框中輸入“男”,單擊確定,出現(xiàn)對話框“查找數(shù)據(jù)在一下單元格中”,單擊確定,對應(yīng)單元格就變成了亮色。