{"不允许更改'connectionstring'属性。连接的当前状态是打开的。“} [英] {"Not allowed to change the 'connectionstring' property. The connection's current state is open."}

查看:121
本文介绍了{"不允许更改'connectionstring'属性。连接的当前状态是打开的。“}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sub SaveData()
        MainForm.Cursor = Cursors.WaitCursor
        Dim SqlStr As String = ""

        'If IsOpenForAlter = True Then
        SqlStr = "UPDATE Assets SET [dbo].[AssetID]=@AssetID [AssetBarcode]=@AssetBarcode,[AssetName]=@AssetName,[AssetType]=@AssetType,[AssetDesc]=@AssetDesc,[Store]=@Store,[AssetLocation]=@AssetLocation,[Manufacture]=@Manufacture,[Brand]=@Brand,[Model]=@Model,[AssetMore]=@AssetMore,[AssetSNo]=@AssetSNo,[AssetTotal]=@AssetTotal,[Status]=@Status,[Condition]=@Condition,[AssetExpiry]=@AssetExpiry,[AssetNotes]=@AssetNotes,[PhotoPath]=@PhotoPath,[AssetVendor]=@AssetVendor,[AssetPurRate]=@AssetPurRate,[AssetScrap]=@AssetScrap,[AssetYear]=@AssetYear,[AssetPurDate]=@AssetPurDate,[AssetServiceStartDate]=@AssetServiceStartDate,[AssetWarranty]=@AssetWarranty,[AssetDepreciation]=@AssetDepreciation,[AssetDepreciationRate]=@AssetDepreciationRate, WHERE AssetName=N'" & OpenedAssetName & "'"
        'Else
        SqlStr = "INSERT INTO [dbo].[Assets] ([AssetID],[AssetBarcode],[AssetName],[AssetType],[AssetDesc],[Store],[AssetLocation],[Manufacture],[Brand],[Model],[AssetMore],[AssetSNo],[AssetTotal],[Status],[Condition],[AssetExpiry],[AssetNotes],[PhotoPath],[AssetVendor],[AssetPurRate],[AssetScrap],[AssetYear],[AssetPurDate],[AssetServiceStartDate],[AssetWarranty],[AssetDepreciation],[AssetDepreciationRate])     VALUES " _
     & " (@AssetID,@AssetBarcode,@AssetName,@AssetType,@AssetDesc,@Store,@AssetLocation,@Manufacture,@Brand,@Model,@AssetMore,@AssetSNo,@AssetTotal,@Status,@Condition,@AssetExpiry,@AssetNotes,@PhotoPath,@AssetVendor,@AssetPurRate,@AssetScrap,@AssetYear,@AssetPurDate,@AssetServiceStartDate,@AssetWarranty,@AssetDepreciation,@AssetDepreciationRate) "

        'End If
        Try
            MAINCON.ConnectionString = ConnectionStrinG
            MAINCON.Open()
            Dim DBF As New SqlClient.SqlCommand(SqlStr, MAINCON)
            With DBF.Parameters

                If Integer.TryParse(TxtAssetID.Text, id) Then
                    DBF.Parameters.AddWithValue("@AssetID", id)
                Else
                    '' The text in the textbox was not a valid integer representation.
                    '' You may have to handle that case here.
                End If
                .AddWithValue("@AssetID", Integer.Parse(TxtAssetID.Text))
                .AddWithValue("@AssetBarcode", TxtAssetBarcode.Text)
                .AddWithValue("@AssetName", TxtAssetName.Text)
                .AddWithValue("@AssetType", TxtAssetType.Text)
                .AddWithValue("@AssetDesc", TxtDescr.Text)
                .AddWithValue("@Store", TxtStoreName.Text)
                .AddWithValue("@AssetLocation", TxtLocation.Text)
                .AddWithValue("@Manufacture", TxtManufacture.Text)
                .AddWithValue("Brand", TxtBrand.Text)
                .AddWithValue("@Model", TxtModel.Text)
                .AddWithValue("@AssetMore", TxtMoreInfo.Text)
                .AddWithValue("@AssetSNo", TxtSerialNumber.Text)
                .AddWithValue("@AssetTotal", TxtQty.Text)
                .AddWithValue("@Status", TxtAssetStatus.Text)
                .AddWithValue("@Condition", txtCondition.Text)
                .AddWithValue("AssetExpiry", TxtExpiry.Value)
                .AddWithValue("@AssetNotes", TxtNote.Text)
                .AddWithValue("@PhotoPath", PhotoPathForLedgers & "\Others\" & TxtAssetName.Text & ".jpg")
                .AddWithValue("@AssetVendor", TxtVendorName.Text)
                .AddWithValue("@AssetPurRate", TxtPurRate.Text)
                .AddWithValue("@AssetScrap", TxtScrapValue.Text)
                .AddWithValue("@AssetYear", TxtYears.Text)
                .AddWithValue("@AssetPurDate", TxtpurchaseDate.Value)
                .AddWithValue("@AssetServiceStartDate", TxtServiceStartDate.Value)
                .AddWithValue("@AssetWarranty", TxtWarrantyDate.Value)
                .AddWithValue("@AssetDepreciation", TxtDepreMethod.Text)
                .AddWithValue("@AssetDepreciationRate", TxtDepRate.Text)


            End With
            DBF.ExecuteNonQuery()
            DBF = Nothing
            MAINCON.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Dim OpenedID As Integer = 1
        Dim cnn As SqlConnection
        cnn = New SqlConnection(ConnectionStrinG)
        cnn.Open()
        Dim ds As New DataSet()
        MainForm.Cursor = Cursors.WaitCursor





我尝试过的事情:



i不知道这个任何人帮我提前谢谢



What I have tried:

i have no idea about this any one help me thanks in advance

推荐答案

查看错误消息:
Quote:

不允许更改'connectionstring'属性。连接的当前状态是打开的。

Not allowed to change the 'connectionstring' property. The connection's current state is open.

它可能不是更清楚:您正在尝试设置打开的SqlConnection对象的ConnectionString属性。这意味着你已经打开它,不知何故,某个地方,你从未关闭它。



在哪里? Dunno - 可以在你的代码中的任何地方。最好的解决方案是丢弃全局conncetion对象,并在需要时创建它们,在使用Statement(Visual Basic)| Microsoft Docs [ ^ ] - 这样,完成后它会自动关闭和处理。

It's couldn't be any clearer: you are trying to set the ConnectionString property of a SqlConnection object that is Open. Which means that you have opened it, and somehow, somewhere, you never closed it.

Where? Dunno - could be anywhere in your code. The best solution is throw away the "global" conncetion object and create them when you need them, inside a Using Statement (Visual Basic) | Microsoft Docs[^] - that way, it is automatically Closed and Disposed when you are finished with it.


这篇关于{"不允许更改'connectionstring'属性。连接的当前状态是打开的。“}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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