2013-04-29 105 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' " & _ 
       ")" 
相關問題