1

에서 얻은 값으로 SQL Server 2012 데이터베이스의 데이터를 업데이트하고 ASP.Net DetailsView의 값을 변경하려고합니다. 나는 강하게 데이터 집합은 TableAdapter에 강력한 형식의 데이터 집합으로 ASP.Net/VB.Net 데이터베이스 업데이트

  • DataSetParentsDetails라고 입력

    • 사용하여 데이터베이스를 업데이트 할 DataTable을가 ParentsDetails라고 ParentsDetailsTableAdapter
    • 라는 것이겠습니까.

    이들은 DataSet 디자이너를 사용하여 만들었습니다.

    은에서 코드 코드 숨김 우리가 데이터베이스에 업데이트 할 양을 알아 내기 위해 사용되는 파일 :

    Protected Sub DetailsViewDetails_ItemCommand(sender As Object, e As System.Web.UI.WebControls.DetailsViewCommandEventArgs) 
        Dim dcmAmountToAdjust As Decimal 
        Dim StrSqlStatement As String 
    
        Select Case e.CommandName 
        Case "Add" 
        Case "Edit" 
         dcmOriginalRegistrationFee = GetValueFromLabelRegistrationFee() 
        Case "Delete" 
        Case "Update" 
         dcmNewRegistrationFee = GetValueFromTextBoxRegistrationFee() 
         dcmAmountToAdjust = dcmNewRegistrationFee - dcmOriginalRegistrationFee 
         ' Update the tuition balance in the parent's data. 
         '------------------------------------------------- 
         StrSqlStatement = 
         "Update Students " & _ 
         "Set RegistrationCode = RegistrationCode + @AmountToAdjust " & _ 
         "Where StudentID = @ID" 
         ' Code to update the database goes here. 
         '--------------------------------------- 
        End Select 
    End Sub 
    

    나는이 전에 여러 번 질문을 받았다 확신하지만 내가 할 수있는 ' StrSqlStatement에있는 쿼리를 사용하여 강력한 형식의 데이터 집합을 통해 데이터베이스를 업데이트하는 방법에 대한 좋은 예를 찾으십시오.

  • +0

    여기에 요점이 없거나 표준 SqlConnection 및 SqlCommand를 사용하면 작업이 수행되지 않습니까? 매개 변수를 전달하고 데이터 유형, 크기 및 필요한 모든 것을 지정할 수 있습니다. – Sean

    +0

    답장을 보내 주셔서 감사합니다. 네, 이것이 데이터베이스를 업데이트하는 가장 쉬운 방법입니다. 코딩 샘플을 보여 주시겠습니까? 감사. –

    답변

    4

    은 첫째로 당신은 연결 문자열이 필요합니다, 그것은 web.config 파일에 연결 문자열을 저장하는 것이 좋습니다는 다음과 같습니다

    <connectionStrings> 
        <add name="MyConnectionString" connectionString="Data Source=putYourServerAndInstanceNameHere;Initial Catalog=putYourDatabaseNameHere;User ID=putYourSqlUsernameHere;Password=password" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
    

    이 루트 <configuration> 요소의 직접적인 자식입니다. 연결 문자열에 대한 자세한 내용은 http://www.connectionstrings.com을 참조하십시오.

    그런 다음 당신은 코드 숨김하여 약간의 수입이 필요합니다, 당신은 이미 그들을 가지고하지 않은 경우 당신은 당신의 프로젝트에 참조로 추가해야합니다 : 다음

    Import System.Data 
    Import System.Data.SqlClient 
    

    우리 데이터베이스에 연결하고 명령을 실행하면 더 안전하기 때문에 매개 변수를 사용합니다.

    Using 문 (여전히 열려 있으면도록 SqlConnection의 폐기 방법은 연결을 닫습니다) 당신을 위해 처리됩니다 여기 conn.Close()을 사용할 필요가 없다는 것을
    'build the connection object using the string from the web.config file 
    Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString) 
        'build the command object specifying the command text and the connection to use, conn 
        Using cmd As New SqlCommand("UPDATE Students SET RegistrationCode = RegistrationCode + @AmountToAdjust WHERE StudentID = @ID", conn) 
        'add the parameters needed by the command 
        cmd.Parameters.AddWithValue("@AmountToAdjust", amountToAdjust) 
        cmd.Parameters.AddWithValue("@ID", studentID) 
        'try to open the connection and execute the statement 
        Try 
         conn.Open() 
         cmd.ExecuteNonQuery() 
        Catch ex As Exception 
         'handle the exception here 
        End Try 
        End Using 
    End Using 
    

    참고.

    +0

    숀, 너 정말 도움이된다! 아주 자세한 도움을 주셔서 감사합니다. 모두들,이 사람에게 "이 답변은 유용합니다"라고 많은 표를하십시오. :-) –

    +0

    대단히 환영합니다 =] – Sean

    관련 문제