如何将数组元素写入数据库
我要将时间通过数组写入数据库表中,我写的程序如下:但是,在运行的时候总是在cmd2.ExecuteNonQuery()这里提示:语法错误(操作符丢失)在查询表达式'201...
我要将时间通过数组写入数据库表中,我写的程序如下:但是,在运行的时候总是在cmd2.ExecuteNonQuery()这里提示:语法错误 (操作符丢失) 在查询表达式 '2010-8-2 15:58:57' 中,请各位高手指教,谢谢!
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim currentTime1(5000) As String '定义数组记录下开始记录时间
Dim i As Integer = 0
Call Iadd()
currentTime1(i) = System.DateTime.Now
Dim ConnectionString As String = "provider=Microsoft.Jet.OLEDB.4.0;" & " Data Source=D:\我的文档\student.mdb" '使用Microsoft.Jet.OLEDB.4.0提供程序连接access数据库
Dim db1 As New OleDb.OleDbConnection(ConnectionString) '创建一个实例db1
db1.Open() '打开数据库,db1是一个实例
'设置语句
Dim queryString As String = "CREATE TABLE 时间记录(开始记录时刻 string PRIMARY KEY,停止记录时刻 string,序号 INT)"
'创建OleDbCommand对象
Dim cmd1 As OleDb.OleDbCommand = New OleDb.OleDbCommand(queryString, db1)
cmd1.ExecuteNonQuery() '执行-创建表
For i = 0 To currentTime1.Length - 1
Dim insert As String = "INSERT INTO 时间记录(开始记录时刻)" & "VALUES(" & currentTime1(i) & ")"
Dim cmd2 As OleDb.OleDbCommand = New OleDb.OleDbCommand(insert, db1)
cmd2.ExecuteNonQuery() '执行-插入数据
Next
db1.Close() 展开
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim currentTime1(5000) As String '定义数组记录下开始记录时间
Dim i As Integer = 0
Call Iadd()
currentTime1(i) = System.DateTime.Now
Dim ConnectionString As String = "provider=Microsoft.Jet.OLEDB.4.0;" & " Data Source=D:\我的文档\student.mdb" '使用Microsoft.Jet.OLEDB.4.0提供程序连接access数据库
Dim db1 As New OleDb.OleDbConnection(ConnectionString) '创建一个实例db1
db1.Open() '打开数据库,db1是一个实例
'设置语句
Dim queryString As String = "CREATE TABLE 时间记录(开始记录时刻 string PRIMARY KEY,停止记录时刻 string,序号 INT)"
'创建OleDbCommand对象
Dim cmd1 As OleDb.OleDbCommand = New OleDb.OleDbCommand(queryString, db1)
cmd1.ExecuteNonQuery() '执行-创建表
For i = 0 To currentTime1.Length - 1
Dim insert As String = "INSERT INTO 时间记录(开始记录时刻)" & "VALUES(" & currentTime1(i) & ")"
Dim cmd2 As OleDb.OleDbCommand = New OleDb.OleDbCommand(insert, db1)
cmd2.ExecuteNonQuery() '执行-插入数据
Next
db1.Close() 展开
3个回答
展开全部
将一个数组存入数据库和使用的语言有关系。
通常的做法是将数组的内容转换后传递到和语言相关的数据处理方法中进行处理。
涉及到的数据转换的方法:
1:可以转换为json格式
[{key:1},{key:2}]
2:可以转换为xml
<values>
<key>1</key><key>2</key>
</values>
数据处理方法解析转换后的字符串,执行数据库保存操作。
通常的做法是将数组的内容转换后传递到和语言相关的数据处理方法中进行处理。
涉及到的数据转换的方法:
1:可以转换为json格式
[{key:1},{key:2}]
2:可以转换为xml
<values>
<key>1</key><key>2</key>
</values>
数据处理方法解析转换后的字符串,执行数据库保存操作。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
关于SQL语句时间格式问题:
一、access中时间要用#号括起来。
二、SQLSERVER及ORACLE
SELECT CONVERT(varchar(100), cdate, 0):24 06 2009 21:57PM
SELECT CONVERT(varchar(100), cdate, 1): 06/24/09
SELECT CONVERT(varchar(100), cdate, 2): 09.06.24
SELECT CONVERT(varchar(100), cdate, 3): 24/06/09
SELECT CONVERT(varchar(100), cdate, 4): 24.06.09
SELECT CONVERT(varchar(100), cdate, 5): 24-06-09
SELECT CONVERT(varchar(100), cdate, 6): 24 06 09
SELECT CONVERT(varchar(100), cdate, 7): 06 24, 09
SELECT CONVERT(varchar(100), cdate,: 10:57:46
SELECT CONVERT(varchar(100), cdate, 9): 06 24 2009 21:57:46:827AM
SELECT CONVERT(varchar(100), cdate, 10): 06-24-09
SELECT CONVERT(varchar(100), cdate, 11): 09/06/24
SELECT CONVERT(varchar(100), cdate, 12): 090624
SELECT CONVERT(varchar(100), cdate, 13): 24 06 2009 21:57:46:937
SELECT CONVERT(varchar(100), cdate, 14): 10:57:46:967
SELECT CONVERT(varchar(100), cdate, 20): 2009-06-24 21:57:47
SELECT CONVERT(varchar(100), cdate, 21): 2009-06-24 21:57:47.157
SELECT CONVERT(varchar(100), cdate, 22): 06/24/09 21:57:47 PM
SELECT CONVERT(varchar(100), cdate, 23): 2009-06-24
SELECT CONVERT(varchar(100), cdate, 24): 10:57:47
SELECT CONVERT(varchar(100), cdate, 25): 2009-06-24 21:57:47.250
SELECT CONVERT(varchar(100), cdate, 100): 06 24 2009 21:57PM
SELECT CONVERT(varchar(100), cdate, 101): 06/24/2009
SELECT CONVERT(varchar(100), cdate, 102): 2009.06.24
SELECT CONVERT(varchar(100), cdate, 103): 24/06/2009
SELECT CONVERT(varchar(100), cdate, 104): 24.06.2009
SELECT CONVERT(varchar(100), cdate, 105): 24-06-2009
SELECT CONVERT(varchar(100), cdate, 106): 24 06 2009
SELECT CONVERT(varchar(100), cdate, 107): 06 24, 2009
SELECT CONVERT(varchar(100), cdate, 108): 10:57:49
SELECT CONVERT(varchar(100), cdate, 109): 06 24 2009 21:57:49:437PM
SELECT CONVERT(varchar(100), cdate, 110): 06-24-2009
SELECT CONVERT(varchar(100), cdate, 111): 2009/06/24
SELECT CONVERT(varchar(100), cdate, 112): 20090624
SELECT CONVERT(varchar(100), cdate, 113): 24 06 2009 21:57:49:513
SELECT CONVERT(varchar(100), cdate, 114): 10:57:49:547
SELECT CONVERT(varchar(100), cdate, 120): 2009-06-24 21:57:49
一、access中时间要用#号括起来。
二、SQLSERVER及ORACLE
SELECT CONVERT(varchar(100), cdate, 0):24 06 2009 21:57PM
SELECT CONVERT(varchar(100), cdate, 1): 06/24/09
SELECT CONVERT(varchar(100), cdate, 2): 09.06.24
SELECT CONVERT(varchar(100), cdate, 3): 24/06/09
SELECT CONVERT(varchar(100), cdate, 4): 24.06.09
SELECT CONVERT(varchar(100), cdate, 5): 24-06-09
SELECT CONVERT(varchar(100), cdate, 6): 24 06 09
SELECT CONVERT(varchar(100), cdate, 7): 06 24, 09
SELECT CONVERT(varchar(100), cdate,: 10:57:46
SELECT CONVERT(varchar(100), cdate, 9): 06 24 2009 21:57:46:827AM
SELECT CONVERT(varchar(100), cdate, 10): 06-24-09
SELECT CONVERT(varchar(100), cdate, 11): 09/06/24
SELECT CONVERT(varchar(100), cdate, 12): 090624
SELECT CONVERT(varchar(100), cdate, 13): 24 06 2009 21:57:46:937
SELECT CONVERT(varchar(100), cdate, 14): 10:57:46:967
SELECT CONVERT(varchar(100), cdate, 20): 2009-06-24 21:57:47
SELECT CONVERT(varchar(100), cdate, 21): 2009-06-24 21:57:47.157
SELECT CONVERT(varchar(100), cdate, 22): 06/24/09 21:57:47 PM
SELECT CONVERT(varchar(100), cdate, 23): 2009-06-24
SELECT CONVERT(varchar(100), cdate, 24): 10:57:47
SELECT CONVERT(varchar(100), cdate, 25): 2009-06-24 21:57:47.250
SELECT CONVERT(varchar(100), cdate, 100): 06 24 2009 21:57PM
SELECT CONVERT(varchar(100), cdate, 101): 06/24/2009
SELECT CONVERT(varchar(100), cdate, 102): 2009.06.24
SELECT CONVERT(varchar(100), cdate, 103): 24/06/2009
SELECT CONVERT(varchar(100), cdate, 104): 24.06.2009
SELECT CONVERT(varchar(100), cdate, 105): 24-06-2009
SELECT CONVERT(varchar(100), cdate, 106): 24 06 2009
SELECT CONVERT(varchar(100), cdate, 107): 06 24, 2009
SELECT CONVERT(varchar(100), cdate, 108): 10:57:49
SELECT CONVERT(varchar(100), cdate, 109): 06 24 2009 21:57:49:437PM
SELECT CONVERT(varchar(100), cdate, 110): 06-24-2009
SELECT CONVERT(varchar(100), cdate, 111): 2009/06/24
SELECT CONVERT(varchar(100), cdate, 112): 20090624
SELECT CONVERT(varchar(100), cdate, 113): 24 06 2009 21:57:49:513
SELECT CONVERT(varchar(100), cdate, 114): 10:57:49:547
SELECT CONVERT(varchar(100), cdate, 120): 2009-06-24 21:57:49
参考资料: http://blog.csdn.net/sharp_future/archive/2008/11/26/3375857.aspx
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
"INSERT INTO 时间记录(开始记录时刻)" & "VALUES(" & currentTime1(i) & ")"
这里是不是要加‘’
VALUES(‘" & currentTime1(i) & "’)"
楼下的currentTime1(i)以及时间记录(开始记录时刻)都是定义的string
这里是不是要加‘’
VALUES(‘" & currentTime1(i) & "’)"
楼下的currentTime1(i)以及时间记录(开始记录时刻)都是定义的string
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |