引用Microsoft SQLDMO Object Library
企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產品對外擴展宣傳的重要窗口,一個合格的網(wǎng)站不僅僅能為公司帶來巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺,創(chuàng)新互聯(lián)公司面向各種領域:成都紙箱等網(wǎng)站設計、營銷型網(wǎng)站解決方案、網(wǎng)站設計等建站排名服務。
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim SQLSER As New SQLDMO.SQLServer
SQLSER.Connect(ServerName, UserName, PassWord) '這三項換為你自己的
PBackup.Database = DatabaseName '數(shù)據(jù)庫名
Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
Dim PathName As String
PathName = "D:\BackUp"
PBackup.Files = PathName
PBackup.SQLBackup(SQLSER)
SQLSER.DisConnect()
SQLSER = Nothing
Me.Cursor = System.Windows.Forms.Cursors.Default
MsgBox("成功備份了數(shù)據(jù)")
End Sub
參考一下下面這段代碼就可以了。
Imports System.Data
'引入數(shù)據(jù)庫操作類命名空間
Imports System.Data.OleDb
'引入ADO.NET操作命名空間
Public Class FrmModifystInfo
Inherits System.Windows.Forms.Form
Public ADOcmd As OleDbDataAdapter
Public ds As DataSet = New DataSet()
'建立DataSet對象
Public mytable As Data.DataTable
'建立表單對象
Public myrow As Data.DataRow
'建立數(shù)據(jù)行對象
Public rownumber As Integer
'定義一個整型變量來存放當前行數(shù)
Public SearchSQL As String
Public cmd As OleDbCommandBuilder
'======================================================
#Region " Windows 窗體設計器生成的代碼 "
#End Region
'======================================================
Private Sub FrmModifystInfo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'窗體的載入
TxtSID.Enabled = False
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False '設置信息為只讀
Dim tablename As String = "student_Info "
SearchSQL = "select * from student_Info "
ExecuteSQL(SearchSQL, tablename) '打開數(shù)據(jù)庫
ShowData() '顯示記錄
End Sub
Private Sub ShowData()
'在窗口中的textbox中顯示數(shù)據(jù)
myrow = mytable.Rows.Item(rownumber)
TxtSID.Text = myrow.Item(0).ToString
TxtName.Text = myrow.Item(1).ToString
ComboSex.Text = myrow.Item(2).ToString
TxtBornDate.Text = Format(myrow.Item(3), "yyyy-MM-dd ")
TxtClassno.Text = myrow.Item(4).ToString
TxtTel.Text = myrow.Item(5).ToString
TxtRuDate.Text = Format(CDate(myrow.Item(6)), "yyyy-MM-dd ")
TxtAddress.Text = myrow.Item(7).ToString
TxtComment.Text = myrow.Item(8).ToString
End Sub
Private Sub BtFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtFirst.Click
'指向第一條數(shù)據(jù)
rownumber = 0
ShowData()
End Sub
Private Sub BtPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtPrev.Click
'指向上一條數(shù)據(jù)
BtNext.Enabled = True
rownumber = rownumber - 1
If rownumber 0 Then
rownumber = 0 '如果到達記錄的首部,行號設為零
BtPrev.Enabled = False
End If
ShowData()
End Sub
Private Sub BtNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtNext.Click
'指向上一條數(shù)據(jù)
BtPrev.Enabled = True
rownumber = rownumber + 1
If rownumber mytable.Rows.Count - 1 Then
rownumber = mytable.Rows.Count - 1 '判斷是否到達最后一條數(shù)據(jù)
BtNext.Enabled = False
End If
ShowData()
End Sub
Private Sub BtLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtLast.Click
'指向最后一條數(shù)據(jù)
rownumber = mytable.Rows.Count - 1
ShowData()
End Sub
Private Sub BtDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtDelete.Click
mytable.Rows.Item(rownumber).Delete() '刪除記錄
If MsgBox( "確定要刪除改記錄嗎? ", MsgBoxStyle.OKCancel + vbExclamation, "警告 ") = MsgBoxResult.OK Then
cmd = New OleDbCommandBuilder(ADOcmd)
'使用自動生成的SQL語句
ADOcmd.Update(ds, "student_Info ")
BtNext.PerformClick()
End If
End Sub
Private Sub BtModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtModify.Click
TxtSID.Enabled = False '關鍵字段只讀
TxtName.Enabled = True '可讀寫
ComboSex.Enabled = True
TxtBornDate.Enabled = True
TxtClassno.Enabled = True
TxtRuDate.Enabled = True
TxtTel.Enabled = True
TxtAddress.Enabled = True
TxtComment.Enabled = True
End Sub
Private Sub BtUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtUpdate.Click
If Not Testtxt(TxtName.Text) Then
MsgBox( "請輸入姓名! ", vbOKOnly + vbExclamation, "警告 ")
TxtName.Focus()
Exit Sub
End If
If Not Testtxt(ComboSex.Text) Then
MsgBox( "請選擇性別! ", vbOKOnly + vbExclamation, "警告 ")
ComboSex.Focus()
Exit Sub
End If
If Not Testtxt(TxtClassno.Text) Then
MsgBox( "請選擇班號! ", vbOKOnly + vbExclamation, "警告 ")
TxtClassno.Focus()
Exit Sub
End If
If Not Testtxt(TxtTel.Text) Then
MsgBox( "請輸入聯(lián)系電話! ", vbOKOnly + vbExclamation, "警告 ")
TxtTel.Focus()
Exit Sub
End If
If Not Testtxt(TxtAddress.Text) Then
MsgBox( "請輸入家庭住址! ", vbOKOnly + vbExclamation, "警告 ")
TxtAddress.Focus()
Exit Sub
End If
If Not IsNumeric(Trim(TxtSID.Text)) Then
MsgBox( "請輸入數(shù)字學號! ", vbOKOnly + vbExclamation, "警告 ")
Exit Sub
TxtSID.Focus()
End If
If Not IsDate(TxtBornDate.Text) Then
MsgBox( "出生時間應輸入日期格式(yyyy-mm-dd)! ", vbOKOnly + vbExclamation, "警告 ")
Exit Sub
TxtBornDate.Focus()
End If
If Not IsDate(TxtRuDate.Text) Then
MsgBox( "入校時間應輸入日期格式(yyyy-mm-dd)! ", vbOKOnly + vbExclamation, "警告 ")
TxtRuDate.Focus()
Exit Sub
End If
myrow.Item(0) = Trim(TxtSID.Text)
myrow.Item(1) = Trim(TxtName.Text)
myrow.Item(2) = Trim(ComboSex.Text)
myrow.Item(3) = Trim(TxtBornDate.Text)
myrow.Item(4) = Trim(TxtClassno.Text)
myrow.Item(5) = Trim(TxtTel.Text)
myrow.Item(6) = Trim(TxtRuDate.Text)
myrow.Item(7) = Trim(TxtAddress.Text)
myrow.Item(8) = Trim(TxtComment.Text)
mytable.GetChanges()
cmd = New OleDbCommandBuilder(ADOcmd)
'使用自動生成的SQL語句
ADOcmd.Update(ds, "student_Info ")
'對數(shù)據(jù)庫進行更新
MsgBox( "修改學籍信息成功! ", vbOKOnly + vbExclamation, "警告 ")
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False '重新設置信息為只讀
End Sub
Private Sub BtCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtCancel.Click
TxtSID.Enabled = False
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False
End Sub
Public Function ExecuteSQL(ByVal SQL As String, ByVal table As String)
Try
'建立ADODataSetCommand對象
'數(shù)據(jù)庫查詢函數(shù)
ADOcmd = New OleDbDataAdapter(SQL, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\student.mdb ")
'建立ADODataSetCommand對象
ADOcmd.Fill(ds, table) '取得表單
mytable = ds.Tables.Item(0) '取得名為table的表
rownumber = 0 '設置為第一行
myrow = mytable.Rows.Item(rownumber)
'取得第一行數(shù)據(jù)
Catch
MsgBox(Err.Description)
End Try
End Function
End Class
下面這段代碼,是我用來計算每個月存500元進銀行,連續(xù)30年,最后連本帶利能有多少錢。這里面涉及復利計算。界面中右邊的文本框用來輸出每一次計算的結果。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
? Dim nianxian As Integer '年限變量
? Dim dingcun As Integer '定存變量
? Dim fuli_big As Long '大復利
? Dim fuli_small As Long '小復利
? Dim i As Integer '循環(huán)變量
? Dim DATAstring As String '數(shù)據(jù)字符串
? nianxian = Val(年限_TextBox.Text)
? dingcun = Val(定存_TextBox.Text)
? DATAstring = ""
? For i = 1 To nianxian
? ? ? fuli_small = dingcun * (1 + 0.1875)
? ? ? dingcun = fuli_small
? ? ? fuli_big = fuli_big + fuli_small
? ? ? DATAstring = DATAstring + "[" + Trim(Str(i)) + "]" + Str(fuli_big) + Chr(13) + Chr(10)
? ? ? 'DATAstring = DATAstring + "[" + Trim(Str(i)) + "]" + Str(fuli_small) + Chr(13) + Chr(10)
? Next
? 'fuli_big = fuli_small
? TextBox1.Text = DATAstring
? 結果_TextBox.Text = Str(fuli_big) + "元"
End Sub
重要的是區(qū)別兩個方法:DbAdapter.Fill是讀,DbCommand.ExecuteNonquery是執(zhí)行修改。刪除按鈕下理論上應先調用修改,確認成功后,再調用讀取。
Imports System.Data.OleDb
Public Class Parking
Private Sub Parking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Now_Timer.Enabled = True
End Sub
Private Sub Now_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Now_Timer.Tick
Now_Time_Label.Text = "當前時間:" Date.Now
End Sub
Private Sub Enter_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Enter_Button.Click
'定義一個OLEDB連接字符串
Dim conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=F:\test\test.mdb"
'實例化OLEDB連接
Dim con As OleDbConnection = New OleDbConnection(conStr)
Dim sql As New System.Text.StringBuilder
'定義數(shù)據(jù)庫插入語句
sql.Append("insert into Time_billing([Car_Num],[Enter_Time])")
sql.Append("values('" Trim(Car_Num_Text.Text) "','" Date.Now "')")
'打開數(shù)據(jù)庫鏈接
con.Open()
'定義執(zhí)行命令
Dim cmd As New System.Data.OleDb.OleDbCommand(sql.ToString, con)
'執(zhí)行命令
cmd.ExecuteNonQuery()
'關閉數(shù)據(jù)庫鏈接
con.Close()
MsgBox("提交成功!")
End Sub
Private Sub Leave_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Leave_Button.Click
Dim Time_Length As Double
Dim Pack_Fee As Double
Dim Enter_time As Date
Dim Leave_time As Date
Dim conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=F:\test\test.mdb"
Dim con As OleDbConnection = New OleDbConnection(conStr)
Dim selSql As New System.Text.StringBuilder
Dim inSql As New System.Text.StringBuilder
Dim upSql As New System.Text.StringBuilder
Dim delSql As New System.Text.StringBuilder
Dim dr As OleDbDataReader
con.Open()
'SQL拼接過程中最后不需要有分號
'在賦值時,讀取字段值時必須使用在數(shù)據(jù)庫客戶端查詢時顯示的字段名
selSql.Append("select")
selSql.Append(" Enter_Time")
selSql.Append(" from [Time_billing]")
selSql.Append("where Car_Num = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim selcmd As New OleDb.OleDbCommand(selSql.ToString, con)
dr = selcmd.ExecuteReader()
If dr.Read() Then
Enter_time = dr("Enter_Time")
Leave_time = Date.Now
Else
MsgBox("木有數(shù)據(jù)!")
End If
Enter_Time_Text.Text = Enter_time
Leave_Time_Text.Text = Leave_time
'求時間差
Time_Length = Math.Round(DateDiff(DateInterval.Minute, Enter_time, Leave_time) / 60, 2)
Pack_Fee = Time_Length * 5
Pack_Fee_Text.Text = Pack_Fee
inSql.Append("update [Time_billing]")
inSql.Append(" set [Leave_Time] = '").Append(Trim(Leave_Time_Text.Text)).Append("'")
inSql.Append(" ,[Packing_Fee] = '").Append(Pack_Fee).Append("'")
inSql.Append("where Car_Num = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim incom As New OleDb.OleDbCommand(inSql.ToString, con)
incom.ExecuteNonQuery()
'con.Close()
MsgBox("結算完成!")
'con.Open()
upSql.Append("insert into Time_billing_History([Car_Num],[Enter_Time],[Leave_Time],[Packing_Fee])")
upSql.Append(" select")
upSql.Append(" [Car_Num]")
upSql.Append(",[Enter_Time]")
upSql.Append(",[Leave_Time]")
upSql.Append(",[Packing_Fee]")
upSql.Append(" from [Time_billing]")
upSql.Append("where [Car_Num] = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim upcom As New OleDb.OleDbCommand(upSql.ToString, con)
upcom.ExecuteNonQuery()
delSql.Append("delete")
delSql.Append(" from")
delSql.Append(" [Time_billing]")
delSql.Append(" where [Car_Num] = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim delcom As New OleDb.OleDbCommand(delSql.ToString, con)
delcom.ExecuteNonQuery()
con.Close()
End Sub
Private Sub Clear_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear_Button.Click
Car_Num_Text.Clear()
Enter_Time_Text.Clear()
Leave_Time_Text.Clear()
Pack_Fee_Text.Clear()
End Sub
End Class
和SQL數(shù)據(jù)庫差不多的,下面是我用vb6.0連接本地mysql數(shù)據(jù)庫的連接字符串
"driver={MySQL?ODBC?3.51?Driver};server=127.0.0.1;database=mysql;uid=root;pwd=sasa"