當前位置:首頁 » 操作系統 » vbnet資料庫實例

vbnet資料庫實例

發布時間: 2023-03-06 16:12:44

Ⅰ 我想把VB.NET連接MYsql資料庫,代碼怎麼寫啊

和SQL資料庫差不多的,下面是我用vb6.0連接本地mysql資料庫的連接字元串

"driver={MySQLODBC3.51Driver};server=127.0.0.1;database=mysql;uid=root;pwd=sasa"

Ⅱ 求用vb.net寫一個讀取資料庫數據的簡單操作。

Option Explicit On
Option Strict On

Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class Program
Public Shared Sub Main()

Dim connectionString As String = _
"Data Source=(local);Initial Catalog=Northwind;" _
& "Integrated Security=true"

' Provide the query string with a parameter placeholder.
Dim queryString As String = _
"SELECT ProctID, UnitPrice, ProctName from dbo.Procts " _
& "WHERE UnitPrice > @pricePoint " _
& "ORDER BY UnitPrice DESC;"

' Specify the parameter value.
Dim paramValue As Integer = 5

' Create and open the connection in a using block. This
' ensures that all resources will be closed and disposed
' when the code exits.
Using connection As New SqlConnection(connectionString)

' Create the Command and Parameter objects.
Dim command As New SqlCommand(queryString, connection)
command.Parameters.AddWithValue("@pricePoint", paramValue)

' Open the connection in a try/catch block.
' Create and execute the DataReader, writing the result
' set to the console window.
Try
connection.Open()
Dim dataReader As SqlDataReader = _
command.ExecuteReader()
Do While dataReader.Read()
Console.WriteLine( _
vbTab & "{0}" & vbTab & "{1}" & vbTab & "{2}", _
dataReader(0), dataReader(1), dataReader(2))
Loop
dataReader.Close()

Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
End Using
End Sub
End Class

這是我在vs2010中微軟自帶的MSDN示例代碼裡面拷的,是關於ADO.net連接sql的操作。
希望對你有幫助。 如果你還需要其他的,我也可以再拷給你看。

Ⅲ VB Net 如何在控制項中顯示資料庫中的數據

//用ADO.net中的Connection進行OLE連接到Access文件,連接字元串例子:
connstr="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATASource=DB文件路徑;JetOLEDB:Databasepassword=密碼"

//再發送Command命令SQL,
Select*fromStudent_PerfomancewhereStu_No='取到的學號'
//再用DataReader取出數據,設置到窗體的控制項上。

Ⅳ 求一個用VB.net操作access資料庫的常式

Imports System
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data.OleDb
Imports System.Data

Public Class Form1
Inherits Form
Private components As System.ComponentModel.Container = Nothing
Private WithEvents lastrec As Button
Private WithEvents nextrec As Button
Private WithEvents previousrec As Button
Private WithEvents firstrec As Button
Private t_books As TextBox
Private t_nl As TextBox
Private t_xb As TextBox
Private t_xm As TextBox
Private t_id As TextBox
Private l_books As Label
Private l_nl As Label
Private l_xb As Label
Private l_xm As Label
Private l_id As Label
Private label1 As Label
Private myDataSet As DataSet
Private WithEvents button1 As Button
Private WithEvents button2 As Button
Private WithEvents button3 As Button
Private WithEvents button4 As Button
Private myBind As BindingManagerBase

Public Sub New ( )
MyBase.New ( )
GetConnected ( )
InitializeComponent ( )
End Sub
'清除在程序中使用過的資源
Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose ( )
End If
End If
MyBase.Dispose ( disposing )
End Sub

Public Sub GetConnected ( )
'創建一個數據連接
Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0 ;
Data Source = db.mdb "
Dim myConn As OleDbConnection = New OleDbConnection ( )
myConn.ConnectionString = strCon
Dim strCom As String = " SELECT * FROM person "
'創建一個 DataSet
myDataSet = New DataSet ( )
myConn.Open ( )
'通過OleDbDataAdapter對象得到一個數據集
Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter
( strCom , myConn )
'把Dataset綁定books數據表
myCommand.Fill ( myDataSet , "person" )
'關閉此數據連接
myConn.Close ( )
End Sub

'插入數據記錄操作代碼
Private Sub button2_Click (ByVal sender As Object , _
ByVal e As System.EventArgs) Handles button2.Click
'判斷所有欄位是否添完,添完則執行,反之彈出提示
If ( t_id.Text <> "" And t_xm.Text <> ""
And t_xb.Text <> "" And t_nl.Text <> ""
And t_books.Text <> "" ) Then
Dim myConn1 As String = " Provider = Microsoft.Jet.OLEDB.4.0 ;
Data Source = db.mdb"
Dim myConn As OleDbConnection = New OleDbConnection ( myConn1 )
myConn.Open ( )
Dim strInsert As String = " INSERT INTO person ( id , xm , xb , nl
, zip ) VALUES ( " & _
t_id.Text + " , '" & _
t_xm.Text + "' , '" & _
t_xb.Text + "' , " & _
t_nl.Text + " , " & _
t_books.Text + ")"
Dim inst As OleDbCommand = New OleDbCommand ( strInsert , myConn )
inst.ExecuteNonQuery ( )
myConn.Close ( )
myDataSet.Tables ( "person" ).Rows ( myBind.Position ).BeginEdit ( )
myDataSet.Tables ( "person" ).Rows ( myBind.Position ).EndEdit ( )
myDataSet.Tables ( "person" ).AcceptChanges ( )
Else
MessageBox.Show ( "必須填滿所有欄位值!" , "錯誤!" )
End If
End Sub

Ⅳ vb.net 寫資料庫

你這是向sql後台資料庫寫數據嗎?
sqldataadapter1.update 改須指定有效的updatecommand,我一直沒有看到
給你個例子你看吧:

'首先指定有效的UpdateCommand
sqlstr = "insert into Playlist(PlayName,PlayPath)values('" & TextBox4.Text.Trim & "','" & TextBox5.Text.Trim & "')"

Mole1.Mycommand.CommandText = sqlstr

'增加新一行(此行必須,要不不能與後台同步,並且後面調用update會出錯)
Mole1.MyDataSet.Tables("Music").Rows.Add()

Mole1.MyDataAdaPter.InsertCommand = Mole1.Mycommand
Mole1.MyDataAdaPter.Update(Mole1.MyDataSet, "Music")

MsgBox("增加數據成功!!")

熱點內容
編程讓我著迷 發布:2025-08-21 05:22:28 瀏覽:763
80d怎麼看存儲 發布:2025-08-21 05:21:39 瀏覽:336
刺激戰場國際服安卓怎麼登不了 發布:2025-08-21 05:14:01 瀏覽:15
混沌序列加密圖像 發布:2025-08-21 04:54:21 瀏覽:955
從哪裡找微博登錄密碼 發布:2025-08-21 04:32:34 瀏覽:444
java輸出三角形 發布:2025-08-21 04:31:43 瀏覽:703
壓縮阻抗 發布:2025-08-21 03:52:28 瀏覽:435
javaopenssl加密 發布:2025-08-21 03:46:51 瀏覽:553
傳智php培訓 發布:2025-08-21 03:46:45 瀏覽:510
我的世界如何在伺服器檢查自己在線時間 發布:2025-08-21 03:45:28 瀏覽:466