且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从VIsual Studio中的数据库获取价值

更新时间:2023-10-08 12:40:22

沃尔特! >

遵循以下步骤:

  • 创建您的连接字符串

将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.