1、首先打開Visual Studio 2008代碼窗口,添加引用。
成都創新互聯于2013年成立,是專業互聯網技術服務公司,擁有項目網站設計、做網站網站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元祁東做網站,已為上家服務,為祁東各地企業和個人服務,聯系電話:13518219792
2、輸入以下代碼:Public conn1 ?As SqlConnection = New SqlConnection 。
3、聲明關鍵字 Public;(因為是全局變量,所以用Public 來聲明)。
4、如果SQL 數據庫就在本機,則用以下代碼連接。
5、如果代碼太長,影響可讀性,可以用空格加"_"后,回車換行即可。
假設cn是你打開的數據,table1中A、B、C值對應的字段為field1,A1、B1、C1對應的字段為field2;table2中A1、B1、C1值對應的字段為field3,輸入框分別為text1
set rs=cn.execute("select field3 from table2 where field3 in (select field2 from table1 where field1='" text1 "'")
if not rs.eof then
msgbox "存在" text1 "和“ rs(0) "的關系"
else
msgbox "不存在" text1 ""的對應關系"
endif
set rs=nothing
這個簡單呀,select?*?from?表名稱?where? 列名稱'全部' (也就是查找某列的值ComboBox當前值,就顯示出所有的記錄了),希望能幫到你
加了單引號就是一個常量字符串了,對于每一行都是一樣的
像這種放在最前面的字段,order by 1 就可以了
dim
myselectquery
as
string
=
"select
*
from
表1
where
姓名='小強'"
dim
mycommand
as
new
sqlcommand
(myselectquery,
conn)
'建立一個command控件,conn是你的sqlconnection對象
conn.open()'打開數據連接
dim
myreader
as
sqldatareader'定義一個reader用來讀數據
myreader
=
mycommand.executereader()'運行你的查詢,結果到myreader
if
myreader.read()
then
'如果查到了數據
msgbox(myreader.getstring(0))
'顯示第一個字段
end
if
以下是完整模塊
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數據庫名字.mdb")
'定義連接
Public
DataBaseRST
As
Integer
'用來返回數據庫執行結果
Public
Function
DataModify(ByVal
str
As
String)
As
Boolean
'進行數據庫修改操作
Dim
cmdinsert
As
New
OleDbCommand
Try
cmdinsert.CommandText
=
str
cmdinsert.Connection
=
cn
If
cn.State
=
ConnectionState.Closed
Then
cn.Open()
DataBaseRST
=
cmdinsert.ExecuteNonQuery()
'用來返回執行的結果
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,用來顯示數據的控件
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
'以下是調用方法
DataModify("
insert
into
aa
values
('1','2')")'-------這里是數據庫更新操作
Search("select
bb
from
aa",DataGridView1)'-----------這里是數據表查詢操作
當前文章:vb.net綜合查詢 vb net
網站地址:http://m.kartarina.com/article22/hiiscc.html
成都網站建設公司_創新互聯,為您提供動態網站、外貿網站建設、定制開發、網站策劃、搜索引擎優化、網站維護
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯