2013-04-29 57 views
0

在这里徘徊一点。尝试开发将用户输入链接到我的数据库的代码,以书目记录的形式。例如,用户会被要求输入他们的姓名地址等等。但我使用的代码似乎并没有执行,因为我不断得到相同的错误。如何通过VB'插入'数据到数据库中

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 
+1

什么错误?您的web.config中的连接字符串实际上是否称为“Database.My.MySettings.Database1ConnectionString1”? – 2013-04-29 13:17:09

+0

ConnectionString“connectionString =”Data Source =。\ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ Database.mdf; Integrated Security = True; User Instance = True“providerName =”System.Data.SqlClient“/>这就是它所说的我的web配置文件? – AmyLouise 2013-04-29 13:26:52

回答

0
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 + "')" 
+0

仍然出现同样的错误。它的第14行有问题。 – AmyLouise 2013-04-29 13:24:27

+0

请发布web.xml中的部分 – Satya 2013-04-29 13:26:24

+0

\t \t AmyLouise 2013-04-29 13:29:57

0

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

con.ConnectionString = my.Settings.Database1ConnectionString1 
1

希望这将有助于你(插入你的代码哪里需要)

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

“我的功能看起来像这样回到

“服务器= ServerExactLocationPath时;数据库=数据库;用户ID =用户名;密码=密码;”

 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 
0
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' " & _ 
       ")" 
相关问题