从VIsual Studio中的数据库获取价值 [英] Get a value from a database in VIsual Studio

查看:140
本文介绍了从VIsual Studio中的数据库获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我正在VB中使用Visual Studio和编程,现在对在解决方案中实现数据库感兴趣.

因此,我所做的就是创建连接

字符串 = "数据源= C:\ Users \ WALTER \ Documents \ Visual Studio 2017 \ Projects \ database_try \ database_try2.sdf"

SqlClient. cn = SqlClient.

问题是我在网上搜索并搜索了如何以一种简单的方式获取在表中创建的某个变量的值的方法,但我没有得到

以一种简单的方式,如果我创建一个表

名称     编号

沃尔特    123

我想知道的是:如何从先前创建的表中获取名称或数字,并将其保存在变量中??

谢谢.

解决方案

沃尔特! >

遵循以下步骤:

  • 创建您的连接字符串

将connectionString设置为String ="Data Source = localhost; ........." 

  • 连接到数据库

将昏暗的连接作为新的SqlConnection(connectionString)
conn.Open()


  • 创建命令和查询

将Dim命令作为新的SqlCommand("SELECT * FROM Client",连接)
昏暗的读者As SqlDataReader = command.ExecuteReader()//执行查询


  • 检索您的结果.有几种方法

将dt dt转换为新的DataTable()
dt.Load(阅读器)

  • 关闭连接

 connection.Close()

有关更多信息:  http://msdn.microsoft.com /en-us/library/system.data.sqlclient.sqlcommand.aspx

希望它会很有用.

请记住,如果有帮助,请将其标记为答案,这将对正在寻找其他信息的人有所帮助 寻求解决相同或相似问题的方法.


Hello everyone.

I am using visual studio and programming in VB, and now I am interested in implementing a database to my solutions.

Thus fur, what I have done is the creation of a connection

Dim connectionString As String = "Data Source=C:\Users\WALTER\Documents\Visual Studio 2017\Projects\database_try\database_try2.sdf"

Dim cn As SqlClient.SqlConnection

cn = New SqlClient.SqlConnection(connectionString)

 

The problem is I have looked and searched on the web about the way to get a value of a certain variable created in a table in a simple manner, but I do not get it.

In a simple way, If I crate a table

Name     Number

Walter     123

What I want to know is: How do I get the name or the number from the previously created table, and save it in a variable.?

Thanks in advance.

解决方案

Hi Walter!

Following these steps : 

  • Create your connection string

Dim connectionString As String = "Data Source=localhost;........."

  • Connect to your Database

Dim connection As New SqlConnection(connectionString)
conn.Open()


  • Create a Command and the query

Dim command As New SqlCommand("SELECT * FROM Client", connection)
Dim reader As SqlDataReader = command.ExecuteReader()  //Execute the Query


  • Retrieve your result. There are several ways

Dim dt As New DataTable()
dt.Load(reader)

  • Close the connection

connection.Close()

For more info : http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx

Hope it will be useful.

Please remember to mark the replies as answers if they help , this will help others who are looking for solutions to the same or similar problem. 


这篇关于从VIsual Studio中的数据库获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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