下面這個(gè)希望可以幫到你~交通燈
為阿城等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及阿城網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、阿城網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
實(shí)例說明在本實(shí)例中,我們將制作一個(gè)具有交通燈效果的應(yīng)用程序。程序運(yùn)行后,交通燈不斷地變化,可以通過按鈕運(yùn)行或停止程序。程序運(yùn)行結(jié)果如圖88-1所示。技術(shù)要點(diǎn)l 不斷變換圖片l 不斷更改時(shí)間間隔 type="text/javascript" type="text/javascript" src=" "
實(shí)現(xiàn)過程■ 新建項(xiàng)目打開Visual Studio.NET,選擇"新建項(xiàng)目",在項(xiàng)目類型窗口中選擇"Visual Basic項(xiàng)目",在模板窗口中選擇"Windows應(yīng)用程序",在名稱域中輸入"Traffic",然后選擇保存路徑。單擊"確認(rèn)"?!?添加控件向當(dāng)前窗體添加一個(gè)Timer控件,用于控制交通燈變換的時(shí)間;三個(gè)Picture控件,分別裝入相應(yīng)的圖片;一個(gè)Button按鈕,用于控制交通燈。屬性保持默認(rèn)值。■ 添加代碼Dim vv As BooleanDim updirec As BooleanPrivate Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Clickvv = Timer1.Enabledvv = Not vvTimer1.Enabled = vvIf vv ThenCommand1.Text = "開 始"ElseCommand1.Text = "停 止"End IfEnd SubPrivate Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.TickStatic i As ShortSelect Case iCase 0Timer1.Interval = 3000picicon0.Visible = Truepicicon1.Visible = Falsepicicon2.Visible = Falsei = i + 1updirec = FalseCase 1Timer1.Interval = 1000picicon0.Visible = Falsepicicon1.Visible = Truepicicon2.Visible = False'Me.Icon = picicon(i).ImageIf updirec Theni = i - 1Elsei = i + 1End IfCase 2Timer1.Interval = 3000picicon0.Visible = Falsepicicon1.Visible = Falsepicicon2.Visible = Truei = i - 1updirec = TrueEnd SelectEnd Sub■ 運(yùn)行程序單擊菜單"調(diào)試|啟動"或單擊 圖標(biāo)運(yùn)行程序。
我在指定的帖子中講過VB.NET中使用ActiveX的方法。所謂ActiveX,也就是現(xiàn)在我們所說的VBA,但是在VBA中,SendCommand方法實(shí)在是不適合使用,比ads_command的功能還差一些。不過話說回來,一些用ARX的高手們都說:“都用ARX了,還用ads_command這樣的東西?”。我比較贊同這個(gè)觀點(diǎn),程序員自己控制所有的操作靈活性比較好。
script type="text/javascript" src="" /script
script type="text/javascript" src="" /script
有兩個(gè)表order(orderid,ordername,totalprice), orderdetail(orderid,productid,productname,price) 已經(jīng)建立一對多
關(guān)聯(lián)(orderid). 用VB.NET建立一個(gè)窗體, 用于主表和明細(xì)表的數(shù)據(jù)錄入及更新.
在窗體上增加兩個(gè)sqldataadapter,分別選擇order和orderdetail表, 并生成數(shù)據(jù)集,在DATASET中建立關(guān)聯(lián)(orderid) 主表中:ORDERID,ORDERNAME 綁定在兩個(gè)TEXTBOX文本框中,用于接受用戶的輸入,totalprice接受明細(xì)表中price
的匯總值.
明細(xì)表綁定到一個(gè)DATAGRID控件, datasource:dsorder; datamember:order.orderorderdetail建立的關(guān)聯(lián)名稱
問題: 在主表中新增一個(gè)記錄,用戶錄入orderid和ordername后,進(jìn)入明細(xì)表的錄入,用戶錄入productid, productname, price后,提示 “明細(xì)orderID列不允許空值,要更正該值嗎?” 是什么原因?
用于連接主表的明細(xì)orderID如果輸入跟主表一樣的orderID時(shí)提示“ForeignKeyconstraint 要求在交叉表中存在子鍵值。要更正該值嗎?”
只有錄入主表中的orderid和ordername后, 先更新主表數(shù)據(jù)源,才可以編輯更新明細(xì)表,這是為什么? 請問,有沒有其他更好的方法, 實(shí)現(xiàn)主表和明細(xì)表的新增記錄同時(shí)更新呢? 謝謝關(guān)照! 網(wǎng)友:方宇 回復(fù):
附部分源代碼:
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
使用向?qū)гO(shè)置數(shù)據(jù)庫連接的好處是省去寫代碼的過程,弊端是不易更改連接
使用代碼連接的好處是需要編寫代碼,但容易更改和維護(hù)
如:數(shù)據(jù)庫的位置變化,直接更改代碼顯然容易一些
代碼:
imports System.data.oledb /*引用命名空間*/
dim cnstr = "provider=microsoft.jet.oledb.4.0,data source=Access數(shù)據(jù)庫文件位置" /*定義連接字符串*/
dim cn as oledbconnection = new oledbconnection(cnstr) /*定義連接*/
cn.open() /*打開連接*/
sqlstr="select... from... 或其他sql"
dim cmd as sqlcommand=new sqlcommand()
cmd.commandtext=sqlstr
cmd.connection=cn
/*以上是建立執(zhí)行語句*/
cmd.executenonquery() 不返回結(jié)果
cmd.executescalar() 返回單個(gè)結(jié)果
cmd.executereader() 返回結(jié)果集
/*以上是執(zhí)行sql語句的方法*/
cn.close() /*關(guān)閉連接*/
你要做用戶驗(yàn)證的話,先根據(jù)用戶名查詢對應(yīng)的密碼,再判斷用戶輸入的密碼和查詢結(jié)果是不是相同就可以了
用cmd.executescalar() 這個(gè)方法
1、實(shí)現(xiàn)上傳按鈕方法代碼。
2、判斷圖片對象是否為空代碼。
3、取得數(shù)據(jù)庫字段 dt.Rows(0)("Pic")方法代碼。
4、字節(jié)數(shù)組轉(zhuǎn)換為Image類型方法代碼。
5、處理SQL中操作Image類型方法代碼。
6、實(shí)現(xiàn)的上傳結(jié)果。
你可以把Textbox2代碼中的C=B*100改成C=CDbl(B)*100,就像Textbox3中的一樣
還有,就是您指的究竟是什么問題?為什么從圖上看不出什么異常呢?
至于文本框中輸入的內(nèi)容,應(yīng)該是什么都可以。只是經(jīng)過val函數(shù)處理后,如果不是純數(shù)字,就會得到一個(gè)0。即val("23.2")=23.2,val("ads")=0