【问题标题】:How to 'insert' data into database via VB如何通过 VB 将数据“插入”到数据库中
【发布时间】:2013-04-23 04:09:03
【问题描述】:

在这里有点挣扎。尝试开发代码以将用户输入链接到我的数据库中,以书籍记录的形式。例如,会要求用户输入他们的姓名地址等。但我使用的代码似乎没有执行,因为我不断收到相同的错误。

Line 12:         Dim con As New SqlConnection
Line 13:         Dim inscmd As New SqlCommand
Line 14:         con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Database.My.MySettings.Database1ConnectionString1").ConnectionString
Line 15:         con.Open()
Line 16:         inscmd.CommandText = ("insert into booking values('" + txtfirstname.Text + "', " + txtSurname.Text + "', " + txtAddressline1.Text + "', " + txtAddressline2.Text + "', " + txtPostcode.Text + "', " + txtTime.Text + "', " + txtPeople.Text + "', " + txtDropoff1.Text + "', " + txtDropoff2.Text + "', " + txtDropoffpost.Text + "")

第 14 行包含错误,但我不知道为什么。这是我的代码;

Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles  btnsubmit.Click
    Dim con As New SqlConnection
    Dim inscmd As New SqlCommand
    con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Database.My.MySettings.Database1ConnectionString1").ConnectionString
    con.Open()
    inscmd.CommandText = ("insert into booking values('" + txtfirstname.Text + "', " + txtSurname.Text + "', " + txtAddressline1.Text + "', " + txtAddressline2.Text + "', " + txtPostcode.Text + "', " + txtTime.Text + "', " + txtPeople.Text + "', " + txtDropoff1.Text + "', " + txtDropoff2.Text + "', " + txtDropoffpost.Text + "")
    Print(inscmd.CommandText)
    inscmd.Connection = con
    inscmd.ExecuteNonQuery()
    con.Close()
    inscmd.Parameters.Clear()

    MsgBox("Your booking has been successfully")
    con.Close()

End Sub

【问题讨论】:

  • 错误是什么?您的 web.config 中的连接字符串实际上是否称为 "Database.My.MySettings.Database1ConnectionString1" ?
  • ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/ > 这是它在我的网络配置文件中所说的吗?

标签: mysql sql vb.net vba


【解决方案1】:

希望这会对您有所帮助(在需要的地方插入您的代码)

        Dim con As New SqlConnection
        Dim myConString As String = getSQLString() ' GET YOUR CON String

' 我的函数返回时是这样的

"Server=ServerExactLocationPath;Database=DataBase;User Id=UserName;Password=Password;"

        Dim objcommand As SqlCommand = New SqlCommand
        'con.ConnectionString = myConString

        With objcommand
            .Connection = con
            Dim cmdText As String = ""
            cmdText = "Insert into SitesStatus (SiteNumber,StatusName,Date,ByUser) values ('" & site & "','" & status & "','" & System.DateTime.Today.ToString("MM/dd/yyyy") & "','" & dbUiInitials & "')"
        'PUT YOUR INSERT ABOVE

         .CommandText = cmdText
        End With
        con.ConnectionString = myConString
        con.Open()
        objcommand.ExecuteNonQuery()
        con.Close()

    Catch ex As Exception
      End Try
 Return Nothing

【讨论】:

    【解决方案2】:
    insert into booking values('" + txtfirstname.Text + "', " + txtSurname.Text + "', " + txtAddressline1.Text + "', " + txtAddressline2.Text + "', " + txtPostcode.Text + "', " + txtTime.Text + "', " + txtPeople.Text + "', " + txtDropoff1.Text + "', " + txtDropoff2.Text + "', " + txtDropoffpost.Text + "
    

    应该是

    insert into booking values('" + txtfirstname.Text + "', '" + txtSurname.Text + "', '" + txtAddressline1.Text + "', '" + txtAddressline2.Text + "', '" + txtPostcode.Text + "', " + txtTime.Text + "', '" + txtPeople.Text + "', '" + txtDropoff1.Text + "', '" + txtDropoff2.Text + "', '" + txtDropoffpost.Text + "')"
    

    【讨论】:

    • 请发布您的 web.xml 的 部分
    • 所以 Database.My.MySettings.Database1ConnectionString1 应该被替换为 DatabaseEntities 或 ConnectionString
    • 这两种方法我都试过了,都不管用。这是我收到的错误“System.NullReferenceException:对象引用未设置为对象的实例。”
    【解决方案3】:

    您应该使用“项目设置”窗口中的连接字符串向导。然后尝试测试连接按钮,确保设置的类型是 ConnectionString 如果设置正确,您应该能够使用此语法获取连接字符串。

    con.ConnectionString = my.Settings.Database1ConnectionString1
    

    【讨论】:

      【解决方案4】:
      strSQL = "INSERT INTO user_account_details" & _
                      "(lastname,firstname,middlename,usertype,reg_date_time,status)" & _
                      " VALUES ( " & _
                      " '" & txtLName.Text & "', " & _
                      " '" & txtFName.Text & "' , " & _
                      " '" & txtMName.Text & "' , " & _
                      " '" & cboUserType.Text & "' , " & _
                      " '#" & Now & "#', " & _
                      " 'Inactive' " & _
                      ")"
      

      【讨论】:

        猜你喜欢
        • 2014-11-05
        • 2017-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多