app.config连接字符串不起作用

时间:2011-12-15 16:16:55

标签: vb.net visual-studio visual-studio-2010

这段代码工作得很好....

Private Sub save()
        Dim con As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Chuttu VB\Projects\LIC\LIC.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")

        Dim sql As New SqlClient.SqlCommand("INSERT INTO ProposerDetails " & _
                                            "VALUES (" & Convert.ToInt32(PolicyNumberTextBox.Text) & ",'" & NameTextBox.Text & "','" & AgeTextBox.Text & "','" & PhoneTextBox.Text & "','" & AddressTextBox.Text & "','" _
                                            & NomineeTextBox.Text & "','" & NomineeRelationTextBox.Text & "'," & PlanID() & ",'" & PolicyTermTextBox.Text & "','" & PremiumAmountTextBox.Text & "','" _
                                            & PremiumTypeComboBox.Text & "','" & SumProposedTextBox.Text & "','Date' )", con)

        MsgBox(sql.CommandText)
        con.Open()

        MsgBox(con.State.ToString)
        Dim i As Integer = sql.ExecuteNonQuery
        MsgBox(i.ToString)
        con.Close()
        sql.Dispose()
        con.Dispose()
        ToolStripStatusLabelMessage.Text = "Saved"
    End Sub

一旦我将连接字符串从app.config更改为连接字符串,它就会停止工作(向DB添加数据)

Private Sub save()
        Dim con As New SqlClient.SqlConnection(LIC.My.Settings.LICConnectionString)

        Dim sql As New SqlClient.SqlCommand("INSERT INTO ProposerDetails " & _
                                            "VALUES (" & Convert.ToInt32(PolicyNumberTextBox.Text) & ",'" & NameTextBox.Text & "','" & AgeTextBox.Text & "','" & PhoneTextBox.Text & "','" & AddressTextBox.Text & "','" _
                                            & NomineeTextBox.Text & "','" & NomineeRelationTextBox.Text & "'," & PlanID() & ",'" & PolicyTermTextBox.Text & "','" & PremiumAmountTextBox.Text & "','" _
                                            & PremiumTypeComboBox.Text & "','" & SumProposedTextBox.Text & "','Date' )", con)

        MsgBox(sql.CommandText)
        con.Open()

        MsgBox(con.State.ToString)
        Dim i As Integer = sql.ExecuteNonQuery
        MsgBox(i.ToString)
        con.Close()
        sql.Dispose()
        con.Dispose()
        ToolStripStatusLabelMessage.Text = "Saved"
    End Sub

注意:我没有错误。

2 个答案:

答案 0 :(得分:1)

这是一个简短的解释。

这是您使用配置文件中的连接字符串的方式。

Dim sqlConn as SqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString)

Here is a link to how to do a parameterized queries

答案 1 :(得分:0)

尝试更改LIC.My.Settings.LICConnectionString 到ConfigurationManager.ConnectionStrings [“LICConnectionString”]。ConnectionString