在VB.NET中将数据从SQL转换为变量的问题 [英] Problem converting data from SQL to a variable in VB.NET
问题描述
(抱歉我的英文不好)
我想转换从SQL服务器中提取的数据(例如INT),并在VB.NET中使用MsgBox打印
例如,在表格中我有列Age,我只选择了一个人的年龄,我想将其转换为我在VB中变暗的变量...
也许如果你看到我尝试了什么就明白我想要什么
谢谢!
我尝试过的事情:
Dim da As New OleDbDataAdapter(SELECT rank FROM Clientes WHERE user ='& TextBox1.Text&'AND password =' & TextBox2.Text&';,cn)
Dim ds As New DataSet
da.Fill(ds,Clientes)
使用DataGridView1
.DataSource = ds.Tables(Clientes)
.Refresh()
End with
MsgBox(Convert.ToInt16(da))'HERE我想打印SELECT的结果
相反,使用OleDbCommand,并调用ExecuteScalar: OleDbCommand .ExecuteScalar方法(System.Data.OleDb) [ ^ ] - 它直接返回单个值,您可以将其强制转换为数字,然后用它做你想做的事。
(Sorry for my bad english)
I want to convert a data extracted from SQL server (For example a INT), and Print that with a MsgBox in VB.NET
For example, in a Table I had the column Age, I Selected just the Age of one person, and I want to convert that to an Variable that I Dim in VB...
Maybe if you see what I tried you understand what I want
Thanks!
What I have tried:
Dim da As New OleDbDataAdapter("SELECT rank FROM Clientes WHERE user='" & TextBox1.Text & "' AND password='" & TextBox2.Text & "';", cn) Dim ds As New DataSet da.Fill(ds, "Clientes") With DataGridView1 .DataSource = ds.Tables("Clientes") .Refresh() End With MsgBox(Convert.ToInt16(da)) 'HERE I WANT TO PRINT THE RESULT OF THE SELECT
Assuming the query is meant to return a single value - and if it doesn't, you need to change it so it does - then don't use a data adapter at all.
Instead, use an OleDbCommand, and call ExecuteScalar: OleDbCommand.ExecuteScalar Method (System.Data.OleDb)[^] - it returns the single value directly, and you can just cast it to a number and do what you want with it.
这篇关于在VB.NET中将数据从SQL转换为变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!