新聞中心
vb.net 中如何使用SQL語句查詢數(shù)據(jù)庫中的數(shù)據(jù)
1、首先打開Visual Studio 2008代碼窗口,添加引用。
成都創(chuàng)新互聯(lián)于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目網(wǎng)站設計、做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元祁東做網(wǎng)站,已為上家服務,為祁東各地企業(yè)和個人服務,聯(lián)系電話:13518219792
2、輸入以下代碼:Public conn1 ?As SqlConnection = New SqlConnection 。
3、聲明關鍵字 Public;(因為是全局變量,所以用Public 來聲明)。
4、如果SQL 數(shù)據(jù)庫就在本機,則用以下代碼連接。
5、如果代碼太長,影響可讀性,可以用空格加"_"后,回車換行即可。
vb.net讀取dataset
假設cn是你打開的數(shù)據(jù),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
VB.NET ComboBox控件如何實現(xiàn)全部查詢
這個簡單呀,select?*?from?表名稱?where? 列名稱'全部' (也就是查找某列的值ComboBox當前值,就顯示出所有的記錄了),希望能幫到你
vb.net 如何對數(shù)據(jù)庫查詢結果記錄集排序?
加了單引號就是一個常量字符串了,對于每一行都是一樣的
像這種放在最前面的字段,order by 1 就可以了
vb.net 中如何使用SQL語句查詢數(shù)據(jù)庫
dim
myselectquery
as
string
=
"select
*
from
表1
where
姓名='小強'"
dim
mycommand
as
new
sqlcommand
(myselectquery,
conn)
'建立一個command控件,conn是你的sqlconnection對象
conn.open()'打開數(shù)據(jù)連接
dim
myreader
as
sqldatareader'定義一個reader用來讀數(shù)據(jù)
myreader
=
mycommand.executereader()'運行你的查詢,結果到myreader
if
myreader.read()
then
'如果查到了數(shù)據(jù)
msgbox(myreader.getstring(0))
'顯示第一個字段
end
if
在vb.net中如何查詢MDB數(shù)據(jù)庫的數(shù)據(jù)
以下是完整模塊
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
'用來返回數(shù)據(jù)庫執(zhí)行結果
Public
Function
DataModify(ByVal
str
As
String)
As
Boolean
'進行數(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í)行的結果
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
'以下是調用方法
DataModify("
insert
into
aa
values
('1','2')")'-------這里是數(shù)據(jù)庫更新操作
Search("select
bb
from
aa",DataGridView1)'-----------這里是數(shù)據(jù)表查詢操作
網(wǎng)站名稱:vb.net綜合查詢 vb net
鏈接地址:http://www.ef60e0e.cn/article/hiiscc.html