【问题标题】:Is there a process in VB.Net similar to the PHP mysql commands?VB.Net 中是否有类似于 PHP mysql 命令的过程?
【发布时间】:2013-02-01 18:29:38
【问题描述】:

我是一名 Web 开发人员,主要处理 PHP 和 MySQL,现在几乎只能在 VB.Net 中进行编程。虽然我过去在 Visual Basic 方面有丰富的经验,但我几乎没有,因为它与 web.xml 有关。学习曲线就在那里,但并不可怕。

我非常熟悉使用 PHP 从 MySQL 数据库中检索信息,但现在我不得不在 VB.Net 中编写代码,并想知道是否有类似的过程不涉及数据控制器的拖放在 Visual Studio 中?

我需要做的是有一个工作页面,在页面加载时检查数据库中的记录,如果在给定时间段内存在一些记录,则将这些记录填充到多个输入字段中。

提前致谢。

【问题讨论】:

标签: sql-server vb.net visual-studio-2010


【解决方案1】:

来自 Microsoft 的 SqlDataReader 示例:

根据您要填充的控件以及显示数据的方式,可能有更好的方法来设置这些控件的数据源,但如果您想读取 MS SQL Server 数据库并处理你自己,做这样的事情:

Option Explicit On 
Option Strict On 

Imports System.Data
Imports System.Data.SqlClient

Module Module1

Sub Main()
    Dim str As String = "Data Source=(local);Initial Catalog=Northwind;" _
   & "Integrated Security=SSPI;"
    ReadOrderData(str)
End Sub 

Private Sub ReadOrderData(ByVal connectionString As String)
    Dim queryString As String = _
        "SELECT OrderID, CustomerID FROM dbo.Orders;" 

    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        connection.Open()

        Dim reader As SqlDataReader = command.ExecuteReader()

        ' Call Read before accessing data. 
        While reader.Read()
            ReadSingleRow(CType(reader, IDataRecord))
        End While 

        ' Call Close when done reading.
        reader.Close()
    End Using 
End Sub 

Private Sub ReadSingleRow(ByVal record As IDataRecord)
   Console.WriteLine(String.Format("{0}, {1}", record(0), record(1)))

End Sub 

End Module

【讨论】:

  • 您能解释一下显式和严格声明的意义吗?
【解决方案2】:

在 .net 中访问数据库的方法有很多种,但最基本的是使用某种类型的 dbcommand。大多数 .net 数据库的连接器都利用了这种行为。在更灵活和 RAD 的方法中,有许多 ORM,最常见的可能是 Entity

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-15
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多