vbnet創建資料庫
我們需要引用ADO.具體的方法是,在"工程" -- "引用" 中,找到"Microsoft ActiveX Data Object *.* Library",這里的"*.*"是指的時ADO的版本號,一般來說,應用程序或ActiveX控制項都具有向下兼容性,所以我們盡可能選擇比較新的版本.以確保程序在能識別舊版本Access的同時,也能識別較新版本的Access.
然後我們需要在程序中創建一個對象.就好比我們在窗體上添加一個FileBox才能看到文件名一樣,只有創建了ADO對象,我們才能夠訪問資料庫.常用的對象有兩個,Connection和Recordset.
創建這兩個對象的具體方法是:
1.在引用後,使用New關鍵字,如
Private Conn As New ADODB.Connection
Private Reco As New ADODB.Recordset
2.在沒引用時,用CreateObject創建對象:
Dim Conn,Reco
Set Conn = CreateObject("ADODB.Connection")
Set Reco = CreateObject("ADODB.Recordset")
創建了對象之後,下一步我們要做的就是打開資料庫了.
先看下面的代碼,可以成功的打開資料庫.
Conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Main.mdb"
這句代碼打開了D盤中的Main.mdb這個資料庫.
Connection.Open方法的第一個參數是連接代碼,它將傳遞給系統的資料庫引擎.前半部分"Provider= Microsoft.Jet.OLEDB.4.0",它表示了資料庫的類型.不同的資料庫可能會不同.後半句"Source=d:\main.mdb"它表示了資料庫所在的絕對路徑.
打開資料庫之後,還要打開表.假如資料庫中有一個表,表名為"Users",欄位有兩個,一個為用戶名,一個為密碼.那麼看以下代碼.
1.想返回"Users"中,[用戶名]為"去年煙花"的[密碼]
Recordset.open "Select 密碼 From Users Where 用戶名='去年煙花'",Connection,1,1
之後我們就可以把用戶輸入的密碼進行比較,看是否允許登錄.
If Recordset.eof and Recordset.bof then
Msgbox "用戶不存在!",16
Else
If PassWord =Recordset("密碼").value then
msgbox "登錄成功!",64
Else
msgbox "密碼錯誤!",32
End If
End If
Recordset.Close
2.假設Admin已經成功登錄系統,我們想把所有的用戶名和密碼都顯示出來
Recordset.open "Select * From Users",Connection,1,1
這時,表已經被打開,我們就用以下代碼把它顯示出來.
Do whlie Not Recordset.eof
Print "用戶名: " & Recordset("用戶名").value & "密碼: " & Recordset("密碼").value
Recordset.MoveNext
Loop
Recordset.Close
由以上代碼示例可以看出,打開表時,可以只打開其中的一個欄位,也可以打開所有.第一個參數是SQL語句.
Select [欄位名] From 表名 [Where 條件]
這里的條件可以省略.且欄位名也可以用"*"來代替所有欄位.
需要注意的是,如果你用(1)中的方法打開,那麼(2)後面顯示的代碼就不能再用在(1)中.因為(1)里並沒有打開[用戶名]欄位,所以這一句Recordset("密碼")就沒有值存在,還有可能出錯.
後面的條件,可以用"="、">"、"<"等運算符.比如 "Where ID > 32".(這里假設[ID]為數字型.)
這是打開的部分.第二個很重要的部分就是查詢記錄.
資料庫它並不是把所有記錄全部放到一個變數中備用的.而是以"當前記錄"的形式來返回一個值.所以我們想從中找到有用的信息,就必須要對信息進行定位/篩選.
定位:
移動到下一條 Recordset.MoveNext
移動到上一條 Recordset.MovePrevious
移動到最後一條 Recordset.MoveLast
移動到第一條 Recordset.MoveFrist
移動到某一條 Recordset.Move Number
篩選:
Recordset.Find "條件"
如:[用方法(2)打開表之後]
Private Sub Command1_Click()
Recordset.Find "用戶名=" & "text1.text"
If Recordset.Eof <> True Then
Msgbox "該用戶的密碼是:" & Recordset("密碼").value,64
Else
Msgbox "未找到該用戶的資料!",16
End If
End Sub
MoveNext 只有當Eof不為True時,才可用,否則發生錯誤.而MovePrevious剛是Bof不為True時....
而只要Eof 和 Bof中有一個不為真時,也就是說只要有一條記錄時,它就可以使用.
Find 方法中的條件和Open時的第一個參數中的條件表述方法是完全一致的.當在已打開的記錄集中,找不到該記錄時,Eof為True.找到則當前的值就是符合條件的記錄.
第三個部分就是添加/修改記錄.
修改記錄很簡單,先按以上的方法找到相關記錄之後,給記錄賦值就可以了.
比如:[(修改密碼)按方法(1)打開表之後]
Recordset("密碼").value = "123456"
Recordset.Updata
需要注意的就是,在修改完成後,要調用Updata方法,這樣修改才能生效.
而添加記錄則可以用以下代碼來實現:
Recordset.addnew
Recordset("用戶名").value = "Admin"
Recordset("密碼").value = "Admin"
Recordset.Updata
這里,先要調用Addnew方法,增加一條新記錄,然後對這個新記錄中的各欄位賦值,最後再調用Updata方法.
到這里就差不多了,最後說一下上面提到的幾個方法.
Recordset.Open SQL語句,數據源,游標類型,打開方法
SQL語句不用說了,就是Select那啥的,目的就是按要求從表中返回數據
數據源就是一個打開之後的Connection對象.
去他媽的游標類型,填1就可以了 [偷笑ing]
打開方法對應了幾個常數,具體哪幾個可以從對象瀏覽器里看.
對應數值的意義:
1 只讀 2 獨占 3 可寫 4 自已可寫,別人可讀
Connection.open 連接代碼,伺服器用戶名,密碼
這里的連接代碼就不在多說了,伺服器用戶名,密碼只有在連接遠程資料庫時才用到.
B. VB.NET通過調用模塊怎麼建立access資料庫
oledb的使用網上的確很多.
如果只是查詢. 寫入. 刪除,我們常用的就是
OleDbConnection '用於寫資料庫的連接
OleDbCommand '用於數據的操作 接收SQL語句 並執行
OleDbDataReader '查詢了,我們就用這個類來讀取查詢的對象
1).OleDbConnection
dim conn as new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source =資料庫的路徑")'連接字元串一般就這樣
查詢前在打開
conn.open
2).OleDbCommand
dim cmd as new OleDbCommand ("SQL語句",conn)'SQL語句決定了你是查詢寫入還是更新刪除
'''
如果是查詢,你還要用到OleDbDataReader
這個是不用新建的不能用New
dim rd as OleDbDataReader=cmd.ExecuteReader'cmd.ExecuteReader 方法就是讀取查詢的內容
cmd.ExecuteNonQuery用於執行操作 返回變動的條數 ,一般用於 添加 刪除 更新 等其它操作
其實就這些了,就是要學點SQL語句就行了.
標准執行過程就是
Dim conn As New OleDbConnection("連接字元串")
conn.Open()
Dim cmd As New OleDbCommand("SQL語句", conn)
'如果是寫入 更新 刪除 則
cmd.ExecuteNonQuery()
' 否則
Dim rd As OleDbDataReader = cmd.ExecuteReader
If rd.Read Then
Dim d As Object = rd.Item("欄位名")
End If
' 結束如果
rd.Close()
cmd.Dispose()
conn.Close()
上面要有 Imports System.Data.OleDb
C. VB.NET如何生成ACCESS資料庫並生成表。 VB.NET
我的需求就是先填寫好表和表中欄位的名稱,然後點擊一個按鈕,就在指定的路徑生成一個ACCESS資料庫。而不是事先手動創建。 請知道的各位附上完整代碼,本人剛接觸VB.NET,有些東西還不是很了解。謝謝。
D. vb.net 建立資料庫(access),並添加一條數據進資料庫的代碼'20分
....你參考參考吧:
資料庫結構:
--------------------------------------
test.mdb [放在BIN目錄下]
表 test(id 自動編號, img OLE)
----------------------------------------------
代碼:
----------------------------------------------------
使用 一個 openfiledialog ,兩個 picturebox
----------------------------------------------------------------------
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Dim cnn As Data.OleDb.OleDbConnection
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.Dispose(True)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
End Sub
Private Sub DBInit()
Try
cnn = New Data.OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=" & Application.StartupPath & "\test.mdb")
cnn.Open()
Catch exp As OleDb.OleDbException
MsgBox(exp.Message)
End
End Try
End Sub
Private Sub DBRelease()
cnn.Close()
cnn = Nothing
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If PictureBox1.Image Is Nothing Then
MsgBox("請先選擇圖片", MsgBoxStyle.Exclamation)
Exit Sub
End If
Dim fs As FileStream = New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim bt(fs.Length) As Byte
fs.Read(bt, 0, fs.Length)
fs.Close()
fs = Nothing
Dim oleCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand
DBInit()
oleCmd.Connection = cnn
oleCmd.CommandType = CommandType.Text
oleCmd.CommandText = "INSERT INTO test (img) VALUES (@img)"
oleCmd.Parameters.Add("@img", OleDb.OleDbType.Binary).Value = bt
oleCmd.ExecuteNonQuery()
oleCmd = Nothing
DBRelease()
MsgBox("圖片插入成功")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim oleCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT img FROM test WHERE id=1")
oleCmd.CommandType = CommandType.Text
DBInit()
oleCmd.Connection = cnn
Dim dr As OleDb.OleDbDataReader = oleCmd.ExecuteReader(CommandBehavior.SingleRow)
If dr.Read Then
If Not IsDBNull(dr.Item(0)) Then
Dim bt() As Byte = dr.Item(0)
Dim ms As MemoryStream = New MemoryStream(bt)
PictureBox2.Image = Image.FromStream(ms)
Else
MsgBox("無圖片")
End If
Else
MsgBox("無數據")
End If
dr.Close()
dr = Nothing
oleCmd = Nothing
DBRelease()
End Sub
End Class
E. 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("增加數據成功!!")
F. vb.net資料庫操作
參考一下下面這段代碼就可以了。
Imports System.Data
'引入資料庫操作類命名空間
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
'建立數據行對象
Public rownumber As Integer
'定義一個整型變數來存放當前行數
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) '打開資料庫
ShowData() '顯示記錄
End Sub
Private Sub ShowData()
'在窗口中的textbox中顯示數據
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
'指向第一條數據
rownumber = 0
ShowData()
End Sub
Private Sub BtPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtPrev.Click
'指向上一條數據
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
'指向上一條數據
BtPrev.Enabled = True
rownumber = rownumber + 1
If rownumber > mytable.Rows.Count - 1 Then
rownumber = mytable.Rows.Count - 1 '判斷是否到達最後一條數據
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
'指向最後一條數據
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( "請輸入聯系電話! ", 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( "請輸入數字學號! ", 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 ")
'對資料庫進行更新
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對象
'資料庫查詢函數
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)
'取得第一行數據
Catch
MsgBox(Err.Description)
End Try
End Function
End Class
G. vb.net 創建ACCESS資料庫。大神給點代碼,學習學習。
這也是我研究很長時間的成果,差不多分得給我吧。\(^o^)/~
Imports System.Data
Public Class Form1
Public dt As New DataTable
Public dr As DataRow
'內存表格及界面表格初始化,分類列表框初始化
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SetDesktopBounds(283, 84, 800, 600)
With dt
.Clear()
.Columns.Add("ID")
.Columns.Add("故障描述")
.Columns.Add("原因分析")
.Columns.Add("排除辦法")
.Columns.Add("備注")
.Columns.Add("時間")
End With
With Me.DataGridView1
.DataSource = dt
.Columns(0).Width = 30
.Columns(1).Width = 120
.Columns(2).Width = 150
.Columns(3).Width = 190
.Columns(4).Width = 65
.Columns(5).Width = 150
End With
With TS_ComboBox1
.Items.Clear()
.Items.Add("電氣")
.Items.Add("機械")
.Items.Add("操作")
End With
End Sub
'將表格中的數據顯示到文本框中
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.) Handles DataGridView1.CellMouseClick
If Not IsDBNull(DataGridView1.CurrentRow.Cells(1).Value) Then
RichTextBox1.Text = DataGridView1.CurrentRow.Cells(1).Value
End If
If Not IsDBNull(DataGridView1.CurrentRow.Cells(2).Value) Then
RichTextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value
End If
If Not IsDBNull(DataGridView1.CurrentRow.Cells(3).Value) Then
RichTextBox3.Text = DataGridView1.CurrentRow.Cells(3).Value
End If
If Not IsDBNull(DataGridView1.CurrentRow.Cells(4).Value) Then
RichTextBox4.Text = DataGridView1.CurrentRow.Cells(4).Value
End If
End Sub
Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click
Dim str
str = MsgBox("您確定要退出系統嗎?", vbOKCancel + vbQuestion, "系統提示")
If str = vbOK Then
End
End If
End Sub
'查看所有記錄
Private Sub MenuItem2_1_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2_1_1.Click
Dim strsql
Dim rs As OleDb.OleDbDataReader
strsql = "select * from Fault_Analyse order by ID"
transactsql(strsql)
rs = oledbcmd.ExecuteReader
dt.Clear()
Do Until Not rs.Read() '在這里作為條件的同時,也執行了該語句
dr = dt.NewRow
dr.Item(0) = rs.Item(0)
dr.Item(1) = rs.Item(2)
dr.Item(2) = rs.Item(3)
dr.Item(3) = rs.Item(4)
dr.Item(4) = rs.Item(5)
dr.Item(5) = rs.Item(1)
dt.Rows.Add(dr)
Loop
DataGridView1.Refresh()
oledbcmd.Dispose() '釋放後不能讀值
dbconnection.Close()
dbconnection.Dispose()
End Sub
'工具欄隱藏與顯示操作
Private Sub MenuItem3_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3_1.Click
If Me.MenuItem3_1.Checked = True Then
Me.MenuItem3_1.Checked = False
Else
Me.MenuItem3_1.Checked = True
End If
If Me.MenuItem3_1.Checked = True Then
ToolStrip1.Visible = True
Else
ToolStrip1.Visible = False
End If
End Sub
'往Access資料庫中插入新行
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Dim strsql As Object
Dim rs As OleDb.OleDbDataReader
Dim datetime As Date
Dim describe_fault, analyse_reason, solution, remarks As String
Dim class_g1, class_g2 As String
Dim i, j As Integer
i = 0 '初始化
describe_fault = RichTextBox1.Text
analyse_reason = RichTextBox2.Text
solution = RichTextBox3.Text
remarks = RichTextBox4.Text
class_g1 = TS_ComboBox1.Text
class_g2 = TS_ComboBox2.Text
datetime = Now
'注意格式
strsql = "select ID from Fault_Analyse" 'where ID =" + ID + ""
transactsql(strsql)
rs = oledbcmd.ExecuteReader
Do Until Not rs.Read() '獲取記錄的行數
i = i + 1
Loop
If i = 0 Then i = 1
For j = 1 To i '查詢表中是否有ID為j的記錄,沒有記錄則定義新行j
strsql = "select ID from Fault_Analyse where ID =" + CStr(j) + ""
transactsql(strsql)
rs = oledbcmd.ExecuteReader
If Not rs.Read() Then
Num1.Value = CInt(j)
Exit For
End If
Next
If TS_ComboBox1.Text <> "" And TS_ComboBox2.Text <> "" Then
strsql = "Insert into Fault_Analyse values('" + CStr(j) + "','" + datetime + " ','" _
+ describe_fault + " ','" + analyse_reason + " ','" + solution + " ','" _
+ remarks + "','" + class_g1 + "','" + class_g2 + "','0')"
transactsql(strsql)
oledbcmd.Dispose() '釋放後不能讀值
dbconnection.Close()
dbconnection.Dispose()
Else
MsgBox("請為輸入的內容分類。", vbOK + vbInformation, "系統提示")
End If
End Sub
'從資料庫中刪除選中行
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
Dim strsql
Dim str
str = MsgBox("確定要刪除- " + CStr(RichTextBox1.Text) + " -的內容嗎?", vbOKCancel + vbQuestion, "系統提示")
If str = vbOK Then
strsql = "delete from Fault_Analyse where describe_fault='" + CStr(RichTextBox1.Text) + "'"
transactsql(strsql)
oledbcmd.Dispose() '釋放後不能讀值
dbconnection.Close()
dbconnection.Dispose()
End If
End Sub
'按照分類查詢資料庫中指定內容
Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
Dim strsql
Dim rs As OleDb.OleDbDataReader
If TS_ComboBox1.Text = "" Or TS_ComboBox2.Text = "" Then
MsgBox("請選擇您要查詢的類別", vbOKCancel + vbInformation, "系統提示")
Else
strsql = "select * from Fault_Analyse where class_g1='" _
+ TS_ComboBox1.Text + "'and class_g2='" + TS_ComboBox2.Text + "' order by ID"
transactsql(strsql)
rs = oledbcmd.ExecuteReader
dt.Clear()
Do Until Not rs.Read() '在這里作為條件的同時,也執行了該語句
dr = dt.NewRow
dr.Item(0) = rs.Item(0)
dr.Item(1) = rs.Item(2)
dr.Item(2) = rs.Item(3)
dr.Item(3) = rs.Item(4)
dr.Item(4) = rs.Item(5)
dr.Item(5) = rs.Item(1)
dt.Rows.Add(dr)
Loop
DataGridView1.Refresh()
oledbcmd.Dispose() '釋放後不能讀值
dbconnection.Close()
dbconnection.Dispose()
End If
End Sub
'界面文本框內容清空
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str
str = MsgBox("確定要清空文本框中內容嗎?", vbOKCancel + vbQuestion, "系統提示")
If str = vbOK Then
RichTextBox1.Text = " "
RichTextBox2.Text = " "
RichTextBox3.Text = " "
RichTextBox4.Text = " "
End If
End Sub
'分類選擇列表框的定義
Private Sub TS_ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TS_ComboBox1.TextChanged
TS_ComboBox2.Text = ""
If TS_ComboBox1.Text = "電氣" Then
With TS_ComboBox2.Items
.Clear()
.Add("程序")
.Add("設備")
.Add("方案")
.Add("工藝")
.Add("其它")
End With
ElseIf TS_ComboBox1.Text = "機械" Then
With TS_ComboBox2.Items
.Clear()
.Add("設計")
.Add("質量")
.Add("故障")
.Add("其它")
End With
ElseIf TS_ComboBox2.Text = "操作" Then
With TS_ComboBox2.Items
.Add("熟練度")
.Add("方案")
.Add("其它")
End With
End If
End Sub
'工具欄的顯示與隱藏
Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
MenuItem3_1.Checked = False
ToolStrip1.Visible = False
End Sub
End Class
H. vb.net怎麼動態創建access資料庫和表
追問: 只有那個辦法? 回答: 是,這跟SQL Server不同,ACCESS是獨立文件的,而且它能支持的SQL語句有限,一般是使用ACCESS來創建,若要動態創建,只能用ADO了,但Sql Server就不同了,它可以執行復雜的SQL語句,相對來說,動態創建資料庫和表,要比ACCESS方便。 追問: 創建表的話能用SQL語句了嗎? 回答: 沒試過,應該不行 追問: 那你能告訴我下怎麼創建表嗎?創建資料庫我會了 回答: 可以使用DAO對象來操作,注意是DAO對象,不是ADO對象! 追問: 能不能詳細說下過程呢?創建表的過程 回答: 創建表的過程用DAO.net和ADO.net都可以,創建資料庫可以用DAO對象,既然你已經可以創建資料庫,那麼創建表只要先連接到這個資料庫,然後用create table語句來執行就行了,可以使用OledbConnection先連接這個庫,接著用OledbCommand對象的ExecuteNonQuery來執行create table語句即可。