參考一下下面這段代碼就可以了。
站在用戶的角度思考問題,與客戶深入溝通,找到漳平網(wǎng)站設(shè)計與漳平網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站制作、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名注冊、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋漳平地區(qū)。
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
'定義一個整型變量來存放當(dāng)前行數(shù)
Public SearchSQL As String
Public cmd As OleDbCommandBuilder
'======================================================
#Region " Windows 窗體設(shè)計器生成的代碼 "
#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 '設(shè)置信息為只讀
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 '如果到達記錄的首部,行號設(shè)為零
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 '關(guān)鍵字段只讀
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ù)字學(xué)號! ", vbOKOnly + vbExclamation, "警告 ")
Exit Sub
TxtSID.Focus()
End If
If Not IsDate(TxtBornDate.Text) Then
MsgBox( "出生時間應(yīng)輸入日期格式(yyyy-mm-dd)! ", vbOKOnly + vbExclamation, "警告 ")
Exit Sub
TxtBornDate.Focus()
End If
If Not IsDate(TxtRuDate.Text) Then
MsgBox( "入校時間應(yīng)輸入日期格式(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( "修改學(xué)籍信息成功! ", 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 '重新設(shè)置信息為只讀
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 '設(shè)置為第一行
myrow = mytable.Rows.Item(rownumber)
'取得第一行數(shù)據(jù)
Catch
MsgBox(Err.Description)
End Try
End Function
End Class
你這是向sql后臺數(shù)據(jù)庫寫數(shù)據(jù)嗎?
sqldataadapter1.update 改須指定有效的updatecommand,我一直沒有看到
給你個例子你看吧:
'首先指定有效的UpdateCommand
sqlstr = "insert into Playlist(PlayName,PlayPath)values('" TextBox4.Text.Trim "','" TextBox5.Text.Trim "')"
Module1.Mycommand.CommandText = sqlstr
'增加新一行(此行必須,要不不能與后臺同步,并且后面調(diào)用update會出錯)
Module1.MyDataSet.Tables("Music").Rows.Add()
Module1.MyDataAdaPter.InsertCommand = Module1.Mycommand
Module1.MyDataAdaPter.Update(Module1.MyDataSet, "Music")
MsgBox("增加數(shù)據(jù)成功?。?)
如果樓主熟悉VB6,可以直接在項目中添加ADODB的Com引用,這樣你就可以像VB6那樣操作數(shù)據(jù)庫了!
另外
.NET
Framework中連接數(shù)據(jù)庫要用到ADO.NET。如果要操作Access數(shù)據(jù)庫,要用到System.Data.OleDb命名空間下的許多類。
比如按樓主所說,“我想在textbox1中顯示表一中【一些數(shù)據(jù)】字段下的第一個內(nèi)容”:
'首先導(dǎo)入命名空間
Imports
System.Data
Imports
System.Data.OleDb
'然后在某一個事件處理程序中寫:
Dim
conn
As
New
OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=數(shù)據(jù)庫.accdb;Jet
OLEDB:Database
Password=MyDbPassword")
Dim
command
As
New
OleDbCommand("Select
*
From
數(shù)據(jù)表",
conn)
conn.Open()
'打開數(shù)據(jù)庫連接
Dim
reader
As
OleDbDataReader
=
command.ExecuteReader()
'執(zhí)行SQL語句,返回OleDbDataReader
對象
Do
While
reader.Read()
'讀取一條數(shù)據(jù)
textbox1.Text
+=
reader("一些數(shù)據(jù)")
VbCrLf
Loop
reader.Close()
'關(guān)閉OleDbDataReader
conn.Close()
'關(guān)閉連接
1、首先打開Visual Studio 2008代碼窗口,添加引用。
2、輸入以下代碼:Public conn1 ?As SqlConnection = New SqlConnection 。
3、聲明關(guān)鍵字 Public;(因為是全局變量,所以用Public 來聲明)。
4、如果SQL 數(shù)據(jù)庫就在本機,則用以下代碼連接。
5、如果代碼太長,影響可讀性,可以用空格加"_"后,回車換行即可。
VB.NET數(shù)據(jù)類型VB.NET中有12種不同的數(shù)據(jù)類型,可以分成三類:數(shù)值類型、文本類型和混合類型。數(shù)值類型是VB.NET數(shù)據(jù)類型的主要類型,共7種。數(shù)值類型的數(shù)據(jù)類型見下表。 數(shù)據(jù)類型表示方式取 值 范 圍說 明整型Integer–2147483648~2147483647用于表示簡單整數(shù)字節(jié)型Byte0~255用于簡單算術(shù)運算。由于這個類型的變量可以在一個字節(jié)中存儲,所以運算速度最快短整型Short–32768~32767是整型的一種形式,相對表示范圍較小長整型Long–9223372036854775808~9223372036854775807是整型的一種形式,相對表示范圍較大單精
度型Single–3.402823E38~–1.401298E-45(對于負數(shù))和1.401298E-45~3.402823E38(對于正數(shù))用于存放單精度浮點數(shù)雙精
度型Double–1.79869313486232E308~–4.94065645841247E-324(對于負數(shù))和4.94065645841247E-324~1.79869313486232E308(對于正數(shù))用于存放雙精度浮點數(shù)小數(shù)Decimal當(dāng)小數(shù)位為0的時候,為–79228162514264337593543950335~
79228162514264337593543950335;當(dāng)小數(shù)位為28的時候,為–7.9228162514264337593543950335~
7.9228162514264337593543950335常用于存儲貨幣值 數(shù)值類型的數(shù)據(jù)類型 用于存放文本的數(shù)據(jù)類型有兩個。文本類型的VB.NET數(shù)據(jù)類型見下表。 數(shù) 據(jù) 類 型表 示 方 式說 明字符串型String用于存放任何形式的字符串,包括一個字符或者多行字符字符型Char用于存放一個字符,它以0~65535之間數(shù)字的形式存儲文本類型的數(shù)據(jù)類型 其中需要說明的是: (1) 對于String類型,可以存放任何形式的字符串,它可以是純粹的文本,也可以是文本和數(shù)字的組合或者是數(shù)字、日期等。例如"This is a book. "和"12345"都是字符串。對于字符串類型的數(shù)據(jù),可以進行相關(guān)的字符串操作,例如連接、截斷等。 (2) 對于Char類型,可以存儲的只是一個字符,注意,這個字符的存儲編碼必需是一個數(shù)字。在計算機系統(tǒng)中,可以顯示很多符號(不僅僅是英文字母),為了能夠正確地顯示,國際上有通用的標(biāo)準(zhǔn)(例如Unicode編碼),為每個字符設(shè)定了一個標(biāo)準(zhǔn)的數(shù)值來表示這個字符。其余的數(shù)據(jù)類型還有:Date數(shù)據(jù)類型、布爾數(shù)據(jù)類型和Object數(shù)據(jù)類型,其說明見下表。 數(shù) 據(jù) 類 型表 示 方 式說 明日期型Date必須用mm/dd/yyyy的格式表示,也可以存儲時間(可以存儲00:00:00~23:59:59之間的任何時間)布爾型Boolean取值為True和False對象型Object 其他數(shù)據(jù)類型 其中需要說明的是:在VB.NET中,對于Boolean類型的數(shù)據(jù),當(dāng)需要把布爾類型的值轉(zhuǎn)換為數(shù)值類型的時候,會把True當(dāng)成1來處理,把False當(dāng)作0來處理。當(dāng)需要把VB.NET數(shù)值類型的值轉(zhuǎn)換為布爾類型的時候,會把0轉(zhuǎn)換為False,而把其他的非0數(shù)值轉(zhuǎn)換為True。
第一個問題,存儲數(shù)據(jù):
使用SqlCommand(如果是Sql的):
Using
cmd
As
New
SqlCommand()
cmd.Connection
=
new
SqlConnection("server=.;database=數(shù)據(jù)庫名;integrated
security=sspi")
cmd.Connection.Open()
cmd.CommandText=string.Format("Insert
into
表名字
Values('{0}','{1}'",您的第一個字符串變量,第二個字符串變量【如果是數(shù)值類型的,不要在索引前加單引號了】)
cmd.NonExecuteQuery();
'獲取數(shù)據(jù)
SqlDataAdapter
adapter
=
new
SqlDataAdapter(cmd);
cmd.CommandText="select
*
from
表"
DataTable
dt
=
new
DataTable()
adapter.Fill(dt)
DataGridView.DataSource
=
dt
End
Using