在VB.NET中将数据从SQL转换为变量的问题 [英] Problem converting data from SQL to a variable in VB.NET

查看:67
本文介绍了在VB.NET中将数据从SQL转换为变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(抱歉我的英文不好)

我想转换从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆