dim
成都創(chuàng)新互聯(lián)公司自成立以來,一直致力于為企業(yè)提供從網(wǎng)站策劃、網(wǎng)站設計、成都網(wǎng)站設計、網(wǎng)站制作、電子商務、網(wǎng)站推廣、網(wǎng)站優(yōu)化到為企業(yè)提供個性化軟件開發(fā)等基于互聯(lián)網(wǎng)的全面整合營銷服務。公司擁有豐富的網(wǎng)站建設和互聯(lián)網(wǎng)應用系統(tǒng)開發(fā)管理經(jīng)驗、成熟的應用系統(tǒng)解決方案、優(yōu)秀的網(wǎng)站開發(fā)工程師團隊及專業(yè)的網(wǎng)站設計師團隊。
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()'運行你的查詢,結(jié)果到myreader
if
myreader.read()
then
'如果查到了數(shù)據(jù)
msgbox(myreader.getstring(0))
'顯示第一個字段
end
if
1、首先打開Visual Studio 2008代碼窗口,添加引用。
2、輸入以下代碼:Public conn1 ?As SqlConnection = New SqlConnection 。
3、聲明關鍵字 Public;(因為是全局變量,所以用Public 來聲明)。
4、如果SQL 數(shù)據(jù)庫就在本機,則用以下代碼連接。
5、如果代碼太長,影響可讀性,可以用空格加"_"后,回車換行即可。
后面要跟上 where 條件 比如update 用戶信息表 set 操作權(quán)限=@type,密碼=@pwd, 用戶名=@name where 用戶信息表ID=@ID
參考一下下面這段代碼就可以了。
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
script type="text/javascript" src="" /script
script type="text/javascript" src="" /script
有兩個表order(orderid,ordername,totalprice), orderdetail(orderid,productid,productname,price) 已經(jīng)建立一對多
關聯(lián)(orderid). 用VB.NET建立一個窗體, 用于主表和明細表的數(shù)據(jù)錄入及更新.
在窗體上增加兩個sqldataadapter,分別選擇order和orderdetail表, 并生成數(shù)據(jù)集,在DATASET中建立關聯(lián)(orderid) 主表中:ORDERID,ORDERNAME 綁定在兩個TEXTBOX文本框中,用于接受用戶的輸入,totalprice接受明細表中price
的匯總值.
明細表綁定到一個DATAGRID控件, datasource:dsorder; datamember:order.orderorderdetail建立的關聯(lián)名稱
問題: 在主表中新增一個記錄,用戶錄入orderid和ordername后,進入明細表的錄入,用戶錄入productid, productname, price后,提示 “明細orderID列不允許空值,要更正該值嗎?” 是什么原因?
用于連接主表的明細orderID如果輸入跟主表一樣的orderID時提示“ForeignKeyconstraint 要求在交叉表中存在子鍵值。要更正該值嗎?”
只有錄入主表中的orderid和ordername后, 先更新主表數(shù)據(jù)源,才可以編輯更新明細表,這是為什么? 請問,有沒有其他更好的方法, 實現(xiàn)主表和明細表的新增記錄同時更新呢? 謝謝關照! 網(wǎng)友:方宇 回復:
附部分源代碼:
Public Class fmOrders
Inherits System.Windows.Forms.Form
Dim WithEvents dtOrderDetails As New DataTable()
Dim BmOrders As BindingManagerBase
Dim BmOrderDetails As BindingManagerBase
Dim OrderDetailsHasChange As Boolean
Dim orderid As DataColumn
dim ordername as datacolumn
Dim SubTotal As DataColumn Private Sub bnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnFirst.Click
BmOrders.Position = 0
End Sub
Private Sub bnPrior_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnPrior.Click
BmOrders.Position -= 1
End Sub
Private Sub bnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnNext.Click
BmOrders.Position += 1
End Sub
Private Sub bnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnLast.Click
BmOrders.Position = BmOrders.Count - 1
End Sub Private Sub bnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnAdd.Click
Try
BmOrders.EndCurrentEdit()
BmOrders.AddNew()
Catch err As System.SystemException
MessageBox.Show(err.ToString)
End Try
End Sub
Private Sub bnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnLoad.Click
FillData()
End Sub Private Sub bnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnUpdate.Click
Dim dsTestOrdersChange As DataSet BmOrderDetails.EndCurrentEdit()
BmOrders.EndCurrentEdit()
If Not dsTestOrders.HasChanges(DataRowState.Deleted) Then
Try
daOrders.Update(dsTestOrders, "Orders")
daOrderDetails.Update(dsTestOrders, "OrderDetails")
dsTestOrders.AcceptChanges()
Catch err As System.SystemException
dsTestOrders.RejectChanges()
MessageBox.Show(err.ToString)
Throw
End Try
Else
Try
daOrderDetails.Update(dsTestOrders, "OrderDetails")
daOrders.Update(dsTestOrders, "Orders")
dsTestOrders.AcceptChanges()
Catch err As System.SystemException
dsTestOrders.RejectChanges()
MessageBox.Show(err.ToString)
Throw
End Try
End If
End Sub Private Sub fmOrders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillData()
dtOrderDetails = dsTestOrders.Tables("OrderDetails")
BmOrders = Me.BindingContext(dsTestOrders, "Orders")
BmOrderDetails = Me.BindingContext(dsTestOrders, "Orders.OrdersOrderDetails")
OrderDetailsHasChange = True
SubTotal = dsTestOrders.Tables("Orders").Columns("SubTotal")
SubTotal.DefaultValue = 0
Price = dsTestOrders.Tables("OrderDetails").Columns("Price")
Price.DefaultValue = 0
End Sub Private Sub FillData()
dsTestOrders.EnforceConstraints = False
daOrders.Fill(dsTestOrders)
daOrderDetails.Fill(dsTestOrders)
dsTestOrders.EnforceConstraints = True
End Sub
Private Sub dtOrderDetails_ColumnChanged(ByVal sender As Object, ByVal e As System.Data.DataColumnChangeEventArgs) _
Handles dtOrderDetails.ColumnChanged Dim drOrders As DataRow
Dim drOrderDetails As DataRow
Dim iSubTotal As Integer
Try
If OrderDetailsHasChange Then
OrderDetailsHasChange = False
drOrders = dsTestOrders.Tables("Orders").Rows(BmOrders.Position)
iSubTotal = 0
For Each drOrderDetails In drOrders.GetChildRows("OrdersOrderDetails")
iSubTotal = iSubTotal + drOrderDetails("price")
Next
drOrders.BeginEdit()
drOrders("SubTotal") = iSubTotal
drOrders.EndEdit()
End If
Finally
OrderDetailsHasChange = True
End Try
End Sub Private Sub bndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bndelete.Click
If BmOrders.Count 0 Then
BmOrders.RemoveAt(BmOrders.Position)
End If
End Sub Private Sub bncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bncancel.Click
BmOrders.CancelCurrentEdit() End Sub
End Class script type="text/javascript" src="" /script
script type="text/javascript" src="" /script