数据库插入的问题(菜鸟级)

goatish 2005-07-04 01:55:01
做了一个数据库插入的函数
执行不了
望各位大侠帮忙
Public Function UpdateDB(ByVal sqlStr As String) As Boolean
Dim sqlconn As New OleDbConnection(connectStr)
Try
Dim cmdTable As OleDbCommand = New OleDbCommand(sqlStr, sqlconn)
cmdTable.CommandType = CommandType.Text
sqlconn.Open()
cmdTable.ExecuteNonQuery()
sqlconn.Close()

Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
Return True
End Function
值取到了
但是到cmdTable.ExecuteNonQuery()就说 Insert into语句语法错误
希不吝赐教
...全文
136 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
renyu732 2005-07-05
  • 打赏
  • 举报
回复
Commmand命令的类型为text文本型.
CommandType
指定如何解释命令字符串。
1.StoredProcedure
受 .NET Framework 精简版的支持。
存储过程的名称。
当 CommandType 属性设置为 StoredProcedure 时,将 CommandText 属性设置为存储过程的名称。当您调用 Command 对象的某个 Execute 方法时,该命令执行此存储过程。

2.TableDirect
受 .NET Framework 精简版的支持。
在将 CommandType 属性设置为 TableDirect 时,应将 CommandText 属性设置为要访问的一个或多个表的名称。如果已命名的任何表包含任何特殊字符,那么用户可能需要使用转义符语法或包括限定字符。当您调用“执行”(Execute) 方法之一时,将返回命名表的所有行和列。
为了访问多个表,请使用逗号分隔的列表(没有空格或空白),其中包含要访问的多个表的名称。当 CommandText 属性命名多个表时,返回指定表的联接。
注意 只有用于 OLE DB 的 .NET Framework 数据提供程序才支持 TableDirect。

3.Text
受 .NET Framework 精简版的支持。
SQL 文本命令。(默认。)
hnzzltj 2005-07-05
  • 打赏
  • 举报
回复
先在执行之前写一个msgbox(SQLstr)检查一下SQL语句吧。
goatish 2005-07-04
  • 打赏
  • 举报
回复
cmdTable.CommandType = CommandType.Text
这句的作用请指教
另外 sql语句在调试工具里是正确的
类似的程序是能执行的
goatish 2005-07-04
  • 打赏
  • 举报
回复
谢谢老代 偶再试试
老戴12345654 2005-07-04
  • 打赏
  • 举报
回复
name 为Access关键字,要用方括号 [name], id 如果为自动增加,就不用出现在下面语句中,如果是数字就不要加' 附上ACCESS保留字段,自己检查一下
http://www.ld-zz.net/articles/detail.asp?thirdID=111

try

sqlstr1 = "INSERT INTO base (id ,[name],comp ,study ,Pservice ,addr ,sex ,Yincome,email ,job ,Another ,sfz ) VALUES ('" & base1.txtId1 & "','" & base1.txtNam1 & "','" & base1.txtCom1 & "','" & base1.txtStu1 & "','" & base1.txtPser1 & " ' ,'" & base1.txtAddr1 & "' ,'" & base1.txtSex1 & "' ,'" & base1.txtYinc1 & "','" & base1.txtEmail1 & "' ,'" & base1.txtJob1 & "' ,'" & base1.txtAno1 & "' ,'" & base1.txtsfz1 & " ') "
UpdateDB(sqlstr1)
karykwan 2005-07-04
  • 打赏
  • 举报
回复
先把你的SQL语句改好吧~太长了.注意字段类型
可以用Response.Write(strSQL)输出来检查一下
goatish 2005-07-04
  • 打赏
  • 举报
回复
语句是对的
但是在程序里老是说语法错误
到cmdTable.ExecuteNonQuery()这执行不过去
pp918 2005-07-04
  • 打赏
  • 举报
回复
这怎么能看出来?
你先把拼好的查询语句放在数据库环境中查询试试
oyljerry 2005-07-04
  • 打赏
  • 举报
回复
先到数据库查询器中试试
goatish 2005-07-04
  • 打赏
  • 举报
回复
Dim cmdTable As OleDbCommand = New OleDbCommand(sqlStr, sqlconn)
在这里已经看见得的值
goatish 2005-07-04
  • 打赏
  • 举报
回复
sqlstr1 = "INSERT INTO base (id ,name,comp ,study ,Pservice ,addr ,sex ,Yincome,email ,job ,Another ,sfz ) VALUES ('" & base1.txtId1 & "','" & base1.txtNam1 & "','" & base1.txtCom1 & "','" & base1.txtStu1 & "','" & base1.txtPser1 & " ' ,'" & base1.txtAddr1 & "' ,'" & base1.txtSex1 & "' ,'" & base1.txtYinc1 & "','" & base1.txtEmail1 & "' ,'" & base1.txtJob1 & "' ,'" & base1.txtAno1 & "' ,'" & base1.txtsfz1 & " ') "
UpdateDB(sqlstr1)
sqlstr2 = "INSERT INTO Bodycondition (id,bmcd,xt,xz,tf,hhb,tz) VALUES ('" & base1.txtId1 & "','" & body1.bmcd & "','" & body1.xt & "','" & body1.xz & "','" & body1.ft & "','" & body1.hhb & "','" & body1.tz & "') "
UpdateDB(sqlstr2)
sqlstr3 = "INSERT INTO puchase (id,time,breed,number,price,money,Znumber) VALUES ('" & base1.txtId1 & "','" & puchase1.time & "','" & puchase1.breed & "','" & puchase1.number & "','" & puchase1.price & "','" & puchase1.money & "','" & puchase1.znum & "') "
UpdateDB(sqlstr3)
sqlstr4 = "INSERT INTO interest (id,football,basketball,pingpang,featherball,swim,gym,music,game,run,internet,book) VALUES ('" & base1.txtId1 & "','" & interest1.football & "','" & interest1.basketball & "','" & interest1.pingpang & "','" & interest1.featherball & "','" & interest1.swim & "','" & interest1.gym & "','" & interest1.music & "','" & interest1.game & "','" & interest1.run & "','" & interest1.internet & "','" & interest1.book & "') "
UpdateDB(sqlstr4)
sqlstr5 = "INSERT INTO response (id,response1,response2,response3,response4 num) VALUES ('" & base1.txtId1 & "','" & res1.response1 & "','" & res1.response2 & "','" & res1.response3 & "','" & res1.response4 & "','" & res1.num & "') "

凨叔 2005-07-04
  • 打赏
  • 举报
回复
SQL语句呢?

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧