如何添加和更新 [英] How to Add and Update

查看:65
本文介绍了如何添加和更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好





Hi All


Dim strSQL3 As String = "SELECT * FROM PeriodDetails"
Me.DaAp1 = New SqlDataAdapter(strSQL3, con)
Dim Dset1 As New DataSet
DaAp1.Fill(Dset1)




If strSQL3 <> "" Then
    For Each dr In Me.List_Class.SelectedItems
        cmd.CommandText = "INSERT INTO PeriodDetails (Class, Section, Day, Period) VALUES ('" & dr & "','" & List_Section.Text & "','" & MTC1.TodayDate & "','" & cboPeriod.Text & "')"
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
    Next
    MsgBox("Recored  Successfully Added")
Else

    For i As Integer = 0 To Dset1.Tables(0).Rows.Count - 1
        cmd.CommandText = ("UPDATE PeriodDetails SET Period='" & cboPeriod.Text & "' WHERE  Class='" & List_Class.Text & "' and Section='" & List_Section.Text & "' and Day='" & CDate(MTC1.SelectionStart) & "'")
    Next i
    con.Open()
    strda = cmd.ExecuteReader()
    con.Close()
    MsgBox("Recored  Successfully Update")
End If



如何使用UPDATE和ADD的条件..我想为上面的代码改变什么





请告诉我该怎么办这个代码。


how to use condition for UPDATE and ADD.. What i want to change for above code


Please Tell me How can i do for this code.

推荐答案

你应该尝试类似的东西下面的示例代码。代码背后的想法是迭代所有类并使用WHERE子句运行select查询,这样您就可以获得与类相关的数据,并且想要更新(并且不想再次插入)。因此,如果在任何类中找到一行,则表示您已经拥有此记录,因此请进入UPDATE块,否则进入INSERT块。



这是示例代码:(请同时查看评论)



You should try something like the below sample code. The idea behind the code is to iterate all the classes and run the select query with the WHERE clause so you can get the data related to the class, you want to update ( and do not want to insert again). So if there is a row found against ANY class, it means you already have this record so go inside the UPDATE block otherwise go inside the INSERT block.

Here is the sample code: (Please check comments also)

For Each dr In Me.List_Class.SelectedItems  ' check against each class

      Dim strSQL3 As String = "SELECT * FROM PeriodDetails where Class = '" & dr & "' and Section='" & List_Section.Text & "' and Day = '" & MTC1.TodayDate & "' and Period = '" & cboPeriod.Text & "'"
      Me.DaAp1 = New SqlDataAdapter(strSQL3, con)
      Dim Dset1 As New DataSet
      DaAp1.Fill(Dset1)

      ' If there is a table exists in dataset and has NO rows, it means the record DOES NOT exists. Insert it.
      If Dset1.Tables.Count > 0 AndAlso Dset1.Tables(0).Rows.Count = 0 Then
          cmd.CommandText = "INSERT INTO PeriodDetails (Class, Section, Day, Period) VALUES ('" & dr & "','" & List_Section.Text & "','" & MTC1.TodayDate & "','" & cboPeriod.Text & "')"
              con.Open()
              cmd.ExecuteNonQuery()
              con.Close()
          MsgBox("Recored  Successfully Added")
      Else  ' Otherwise update the record with the class you have selected
          For i As Integer = 0 To Dset1.Tables(0).Rows.Count - 1
              cmd.CommandText = ("UPDATE PeriodDetails SET Period='" & cboPeriod.Text & "' WHERE  Class='" & List_Class.Text & "' and Section='" & List_Section.Text & "' and Day='" & CDate(MTC1.SelectionStart) & "'")
          Next i
          con.Open()
          strda = cmd.ExecuteReader()
          con.Close()
          MsgBox("Recored  Successfully Update")
      End If
 Next





希望有帮助!



如果您需要进一步的帮助,请与我们分享。



Hope it helps!

If you need further assistance please share with us.


Hi Guys,



我是asp.net的新手。作为初学者,我创建了一个示例欢迎页面并将其保存在.htm中。但是,当我尝试在Windows 7 64中使用IIS发布它时出现了问题。请参阅下面的错误消息:



RockMelt对localhost的连接尝试被拒绝。网站可能已关闭,或者您的网络可能配置不正确。



希望有人可以帮助我。



提前感谢!
Hi Guys,

I am new in asp.net. as a beginner, i created a sample welcome page and save it in .htm. however, when i try to publish it using IIS in windows 7 64 something went wrong. see below for error message:

RockMelt''s connection attempt to localhost was rejected. The website may be down, or your network may not be properly configured.

Hope someone could help me.

thanks in advance!


这篇关于如何添加和更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆