我收到異常錯誤'無效列名'SQL Server無效列名異常
但如果在插入它時使用整數正在接受。
請幫助我新的VB.NET中
這裏是代碼
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Student
Dim cs As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Example\Student.mdf;Integrated Security=True;User Instance=True")
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Private Sub Student_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'StudentDataSet1.Student' table. You can move, or remove it, as needed.
Me.StudentTableAdapter1.Fill(Me.StudentDataSet1.Student)
'TODO: This line of code loads data into the 'StudentDataSet.Student' table. You can move, or remove it, as needed.
Me.StudentTableAdapter.Fill(Me.StudentDataSet.Student)
cmd.Connection = cs
End Sub
Private Sub StudentBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StudentBindingNavigatorSaveItem.Click
Me.Validate()
Me.StudentBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.StudentDataSet)
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
StudentBindingSource.AddNew()
USNTextBox.Focus()
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Try
Me.Validate()
Me.StudentBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.StudentDataSet)
MsgBox("1 record is added")
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
If USNTextBox.Text <> "" And NameTextBox.Text <> "" And MarksTextBox.Text <> "" Then
cs.Open()
cmd.CommandText = "INSERT INTO Student" & "(USN, Name, Marks)" & "VALUES (" & USNTextBox.Text & ", " & NameTextBox.Text & ", " & MarksTextBox.Text & ")"
cmd.ExecuteNonQuery()
cs.Close()
USNTextBox.Text = ""
NameTextBox.Text = ""
MarksTextBox.Text = ""
End If
End Sub
End Class
儘量引用並顯示SQL命令你將要執行和看哪裏錯了。您可以通過使用參數來防止該問題(以及更多!)。 –