您的位置:程序门 -> .net技术 -> vb.net



新手請問各位:數據庫為何不會更新?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


新手請問各位:數據庫為何不會更新?
发表于:2008-01-22 10:27:09 楼主
請教各位好心人:
        我是一名初學者,去書店抱了一本 <visual   basic   2005入門與提高> 的書(清華大學出版社出版),我按他上面的源代碼做了數據連接並更新卻無法更新數據庫,從網上下載了這本書的源代碼學習程序也不能更新數據庫!!!----datagrid顯示已更新,但關閉程序重新再開則數據仍沒更新.
        此書源代碼如下:
imports   system
imports   system.reflection
imports   system.data.sqlclient
imports   system.data
'數據連接(已測試通過)
public   class   form1
        dim   connstring   as   string   =   "data   source=ken;initial   catalog=pubs;integrated   security=true"
        dim   sqlstring   as   string   =   "select   *   from   authors"
        dim   sqldbconn1   as   new   system.data.sqlclient.sqlconnection(connstring)
        dim   adapter1   as   new   sqldataadapter(sqlstring,   sqldbconn1)
        dim   dataset1   as   new   system.data.dataset
        dim   cb_adapter1   as   sqlcommandbuilder   =   new   system.data.sqlclient.sqlcommandbuilder(adapter1)
        dim   bindingsource1   as   bindingsource
'以上數據連接部分連接正常

'刪除語句(buttn1第一個按鈕)
if   (me.bindingcontext(dataset1,   "authors").count   >   0)   then
                        me.bindingcontext(dataset1,   "authors").removeat(me.datagridview1.currentrow.index)

'更新語句(buttn2第二個按鈕)
dim   num   as   integer
                '礿砦絞?腔?睡晤憮﹝
                me.bindingcontext(dataset1,   "authors").endcurrentedit()
                '鳳?dataset笢載蜊腔囀?
                dim   chgobj   as   new   datatable
                chgobj   =   ctype(dataset1.tables("authors").getchanges,   datatable)
                '?脤載蜊腔囀?祥峈諾ㄛ憩硒俴悵湔紱釬
                if   not   chgobj   is   nothing   then
                        '湖羲杅擂蟀諉
                        if   sqldbconn1.state   =   0   then   sqldbconn1.open()
                        adapter1.continueupdateonerror   =   true
                        try
                                num   =   adapter1.update(chgobj)
                                dataset1.merge(chgobj)
                                'dataset1.acceptchanges()
                                adapter1.update(chgobj)
                        catch   eload   as   system.exception
                                system.windows.forms.messagebox.show(eload.message)
                        end   try
                        dataset1.tables("authors").clear()
                        adapter1.fill(dataset1,   "authors")
                        '壽敕杅擂蟀諉
                        sqldbconn1.close()
                end   if
发表于:2008-01-22 16:31:511楼 得分:0
沒人回復啊,自已先頂一下
发表于:2008-01-22 17:01:182楼 得分:0
'刪除語句(buttn1第一個按鈕)
if       (me.bindingcontext(dataset1,       "authors").count       >       0)       then
    me.bindingcontext(dataset1,       "authors").removeat(me.datagridview1.currentrow.index)  
========================
看到這裡,汗就一個字呀。
刪除應該是這樣的:
    using   cmd   as   new   sqlclient.sqlcommand
                                                        with   cmd
                                                                .connection   =   sqldbconn1      
                                                                .commandtype   =   commandtype.text
                                            .commandtext="delete   authors     where   數據庫中的字段名='你想刪的那條數據'   "                                                                               .EXECutenonquery()
                                                        end   with
                                                end   using
发表于:2008-01-22 17:02:523楼 得分:0
其它的就在.commandtext這裡換個sql語句就可以了。。樓主看看sql的書。。有幫助。
发表于:2008-01-22 17:16:194楼 得分:0
commandtext="添加你的sql语句"
发表于:2008-01-22 17:34:085楼 得分:0
commandtext="添加你的sql语句"
发表于:2008-01-23 16:31:366楼 得分:0
2楼是对得
发表于:2008-01-24 10:52:197楼 得分:0
謝謝2樓正解,辛苦各位了,先給點分吧.
書本誤導了本是菜鳥的我,看來書上說的也有假了.
高分求數據庫的新增,修改,刪除,更新語句,要實用的(能更新到數據庫資料)最好不要象書上說的中看不中用,謝謝了.
发表于:2008-01-24 10:59:458楼 得分:0
咦,怎麼給分呀@_@,真菜到家了
发表于:2008-01-24 11:06:469楼 得分:0
你看1楼上面的右侧,有"管理"按钮,你点管理,然后回出现给分的分数输入框,在里面写分数,最后输入你的登录口令进行给分。
发表于:2008-01-24 11:09:0610楼 得分:0
'dataset1.acceptchanges()  
-----------------
你把它注释了它怎么更新...先好好看看dataset怎么用...

很多书是不负责任...但这个例子却是对的...人家讲dataset讲binding的2-6楼拿sql语句来不是也在误导楼主吗...
发表于:2008-01-24 11:11:1411楼 得分:0
結帖方法請參考:http://topic.csdn.net/u/20080110/19/7cb462f1-cac6-4c28-848e-0a879f4fd642.html

高分求數據庫的新增,修改,刪除,更新語句,
————————————————————
修改和更新一個意思,都是update。

我把我的實例給你參考下,寫的不是很漂亮,但是可以用。
sql code
--刪除 delete from iodata_most where (holdername = @original_員工姓名) and (emp_number = @original_員工工號) and (departmentno = @original_部門編號) and (iodate = @original_出入日期) and@isnull_早班進入 = 1 and time1 is null or time1 = @original_早班進入) and@isnull_早班外出 = 1 and time2 is null or time2 = @original_早班外出) and@isnull_午班進入 = 1 and time3 is null or time3 = @original_午班進入) and@isnull_午班外出 = 1 and time4 is null or time4 = @original_午班外出) and@isnull_晚班進入 = 1 and time5 is null or time5 = @original_晚班進入) and@isnull_晚班外出 = 1 and time6 is null or time6 = @original_晚班外出) --插入 insert into iodata_most (holdername, emp_number, departmentno, iodate, time1, time2, time3, time4, time5, time6, latetime, leaveearly, overtime) values@員工姓名,@員工工號,@部門編號,@出入日期,@早班進入,@早班外出,@午班進入,@午班外出,@晚班進入,@晚班外出,@遲到時間,@早退時間,@加班時間) --更新 update iodata_most set holdername = @員工姓名, emp_number = @員工工號, departmentno = @部門編號, iodate = @出入日期, time1 = @早班進入, time2 = @早班外出, time3 = @午班進入, time4 = @午班外出, time5 = @晚班進入, time6 = @晚班外出 where (holdername = @original_員工姓名) and (emp_number = @original_員工工號) and (departmentno = @original_部門編號) and (iodate = @original_出入日期) and@isnull_早班進入 = 1 and time1 is null or time1 = @original_早班進入) and@isnull_早班外出 = 1 and time2 is null or time2 = @original_早班外出) and@isnull_午班進入 = 1 and time3 is null or time3 = @original_午班進入) and@isnull_午班外出 = 1 and time4 is null or time4 = @original_午班外出) and@isnull_晚班進入 = 1 and time5 is null or time5 = @original_晚班進入) and@isnull_晚班外出 = 1 and time6 is null or time6 = @original_晚班外出)

发表于:2008-01-24 11:31:0412楼 得分:0
樓主好像弄錯了,你把帖子從20分加到了120分,這個並不是給分結帖。
帖子的相關操作請參考:


快速检索

最新资讯
热门点击