我有一个双重类型值列表,我希望将其插入到datatable中,并使用datetime标头将其绑定到datagridview。那我怎么能像这样绑定这个列表
列出项目:
1234
147845689
在数据表中 2015年1月12日02/2/2015 03/2/2015
1234 1478 45689
感谢您的帮助
答案 0 :(得分:0)
不确定日期与您的列表的关系,但是......
Dim l As New List(of String)()
l.Add("1234")
l.Add("5678")
l.Add("9012")
Dim columns as New List(of String)()
Dim data as New List(of String)()
Dim ddl as New StringBuilder(1000)
ddl.Append("Create Table MySchema.MyTable (")
For i As integer = 0 To l.Count - 1
ddl.Append("Column" & (i + 1).ToString() & " varchar(100)")
If i < l.Count -1 Then
ddl.Append(",")
End If
columns.Add("Column" & (i + 1).ToString())
data.Add("'" & l(i) & "'")
Next
ddl.Append(")")
'--- Some ADO code here to create connection/command
. . . . . .
'---
command.CommantText = ddl.ToString()
command.ExecuteNonQuery()
Dim sql as String = "Insert into MySchema.MyTable ({0}) Values ({1})"
sql = String.Format(sql, string.Join(",", columns.ToArray()), string.Join(",", data.ToArray()))
command.CommantText = sql
command.ExecuteNonQuery()
免责声明未对COde进行测试