Checkedlistbox项目文本作为列进入SQL表 [英] Checkedlistbox item text into SQL table as column
问题描述
所以我要做的是让我的程序允许用户从已检查的列表框中选择他们希望在他们的数据库中拥有哪些列。到目前为止,我已将代码更改为将checkedlistbox格式化为字符串的位置,问题是我无法将文本分成单个实例,而是从所有选中的框中获取文本并将它们网格化为一个长字符串并仅生成一列。
So what I am trying to do is have my program allow users to pick from a checked listbox what columns they would like to have in their database. so far I have changed the code to where it formats the checkedlistbox into a string, problem is I can not separate the text into individual instances instead it take the text from all the checked boxes and meshes them into one long string and makes only one column.
Dim cb As New System.Text.StringBuilder
For Each item In CheckedListBox1.CheckedItems
cb.Append(item).Append("")
cb.ToString.Split()
Next
sqlcon.Open()
Dim cmd As SqlClient.SqlCommand
Dim sql As String
sql = "CREATE Table " + InputBox("Name of Table ") + "(Id INTEGER Not Null PRIMARY KEY CLUSTERED ([Id] ASC)," + cb.ToString + " NVARCHAR (MAX) NULL)"
我是什么尝试过:
我试图改变拆分功能并试图将追加字符串格式改为某些东西我可以使用split函数,但无效,它仍然会产生一个长字符串或者我收到错误。
What I have tried:
I have tried to change the split function and have tried to change the append string format into something I could use the split function with, but to no avail, it still produces one long string or I receive an error.
推荐答案
尝试这样的事情:
Try something like this:
Dim cb As New System.Text.StringBuilder("CREATE TABLE");
Dim tableName As String = InputBox("Name of table ")
cb.AppendFormat(" [{0}] ", tableName.Replace("]", "]]"))
cb.Append("(Id INTEGER Not Null PRIMARY KEY CLUSTERED ([Id] ASC)")
For Each item In CheckedListBox1.CheckedItems
Dim columnName As String = Convert.ToString(item)
cb.AppendFormat(", [{0}] nvarchar(max) NULL", columnName.Replace("]", "]]"))
Next
cb.Append(")")
Dim sql As String = cb.ToString()
这篇关于Checkedlistbox项目文本作为列进入SQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!