插入数据到访问数据库时插入命令中的错误

时间:2013-09-24 10:29:11

标签: vb.net

此命令对于插入单个值是正确的,

 "INSERT INTO test (testname) values ('" & txtSelect.Text & "')"

实际上我正在尝试通过此命令插入,但它无法正常工作..

 "INSERT INTO AdmitPt(Bedcategory, BedNo, BedCharges, PtName, PtAge, Address, PhoneNo, 
 Date, BloodGroup, Doctor, Remarks) VALUES('" & CmbBedType.SelectedItem & "', '" & 
 CmbBedNo.SelectedItem & "', " & txtCharges.Text & "', '" & txtPatName.Text & "', '" & 
 txtPatAge.Text & "', '" & txtPatAdd.Text & "', '" & txtPhone.Text & "', '" & 
 dtpDate.Value.ToShortDateString & "', '" & cmbBloodGrp.SelectedItem & "', '" & 
 cmbDoctor.SelectedItem & "',  " & txtRemarks.Text & ")"
好心纠正我,我犯了一些错误。

3 个答案:

答案 0 :(得分:0)

您应该使用Text

SelectedItem属性
 "INSERT INTO AdmitPt(Bedcategory, BedNo, BedCharges, PtName, PtAge, Address, PhoneNo, 
  Date, BloodGroup, Doctor, Remarks) VALUES('" & CmbBedType.SelectedItem.Text & "', '" & 
 CmbBedNo.SelectedItem.Text & "', " & txtCharges.Text & "', '" & txtPatName.Text & "', '" & 
 txtPatAge.Text & "', '" & txtPatAdd.Text & "', '" & txtPhone.Text & "', '" & 
 dtpDate.Value.ToShortDateString() & "', '" & cmbBloodGrp.SelectedItem.Text & "', '" & 
 cmbDoctor.SelectedItem.Text & "', " & txtRemarks.Text & ")"

ToShortDateString也是一种方法,应该写成ToShortDateString()

答案 1 :(得分:0)

没有生成异常,但值不会转到DB ..没有存储..

code

   Dim RegNo, BedNo, BedType, Charges, PatName, PatAge, PatAddr, Phone, CheckupDate, BloodGroup, Doctor, Remarks As String


    RegNo = txtRegNo.Text
    BedNo = CmbBedNo.SelectedItem.ToString()
    BedType = CmbBedType.SelectedItem.ToString()
    Charges = txtCharges.Text
    PatName = txtPatName.Text
    PatAge = txtPatAge.Text
    PatAddr = txtPatAdd.Text
    Phone = txtPhone.Text
    CheckupDate = dtpDate.Value.ToShortDateString()
    BloodGroup = cmbBloodGrp.SelectedItem.ToString()
    Doctor = cmbDoctor.SelectedItem.ToString()
    Remarks = txtRemarks.Text

    conStudent.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\DBProject\hspms.mdb"
    conStudent.Open()
    comStudent.CommandText = "INSERT INTO AdmitPt(ID, Bedcategory, BedNo, BedCharges, PtName, PtAge, Address, PhoneNo, Date, BloodGroup, Doctor, Remarks) VALUES('" & RegNo & "', '" & BedType & "', '" & BedNo & "', " & Charges & "', '" & PatName & "', '" & PatAge & "', '" & PatAddr & "', '" & Phone & "', '" & CheckupDate & "', '" & BloodGroup & "', '" & Doctor & "',  " & Remarks & ")"
    comStudent.Connection = conStudent
    comStudent.CommandType = CommandType.Text
    conStudent.Close()

code

答案 2 :(得分:0)

如果您发布了整个代码 - 您忘记实际执行命令。添加

comStudent.ExecuteNonQuery()

关闭连接之前。