通过使用选择获取表名

  • 本文关键字:获取 选择 vb.net
  • 更新时间 :
  • 英文 :

connection.Open()
Dim adapter1 As New MySqlDataAdapter(TextBox1.Text, connection)
Dim table As New DataTable()
adapter1.Fill(table)
DataGridView1.DataSource = table

我想检测从 textbox1.text 中的查询中选择的表名 经验

TextBox1.Text = "select * from Account_ID where entry = 123456"

所以在这种情况下,表名是: Account_ID 但如果是别的东西,我怎么能得到表名..尝试了斯普利特,但仍然没有给出一个好的结果,所以有人知道吗?

我试过这个并且它有效(如果您的连接字符串保持不变,我的意思是如果您不使用列枚举 SELECT 更改"*"选择(:

Dim words As String() = TextBox1.Text.Split(New Char() {" "c})
MessageBox.Show(words(3)) ''words(3) is Account_ID in your case

希望对您有所帮助。 ^^

嗯,我非常了解这个问题。我知道 Idea 在使用 DataAdapter 时,数据表对象不会填充其名称。

因此,如果我确定只会使用简单的查询,我会使用正则表达式来查找表名:

Dim TableName As String = System.Text.RegularExpressions.Regex.Match(TextBox1.Text, "(?<=from )S+", Text.RegularExpressions.RegexOptions.IgnoreCase Or Text.RegularExpressions.RegexOptions.Singleline).Value

但是,如果你真的需要确定,你将需要一个SQL-Lexer并解析代码以找到alle表名。不幸的是,我不知道。

也许看看Microsoft.SqlServer.Management命名空间。

最新更新