首页 \ 问答 \ VB.Net连接(VB.Net connection)

VB.Net连接(VB.Net connection)

我正在慢慢转换到VB.Net,看起来最简单的东西是完全不同的。 我试图连接到SQL Server,只是抓住一些数据。 我测试了连接,并没有问题。 现在我试图简单地从服务器中选择一些数据,并在Msgbox中显示第一个名字(或任何有关此事的信息)。 这是我到目前为止的代码....

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
    Public conn As New SqlConnection("data source=Tuys1;initial catalog=DDDBuyer; user ID=userID; password=password")
    Private dA As New SqlDataAdapter("Select FirstName, LastName from tblBuyers where Buyerid=1473", conn)
    Private dS As New DataSet
End Class

我不确定如何从这里出发......我知道有这样的事情

.hasRows

if ds.hasrows then
   msgbox = "HELLO"
End if

或者像在txtFirstName中显示名字一样

我试图弄清楚它是如何工作的,经过一些研究后,很难找到一个能够帮助我完成我想要完成的具体示例。


I'm slowly transitioning to VB.Net and it seems like the simplest of things are completely different. I'm trying to do a connection to SQL Server and just grab some data. I've tested the connection and it's fine. Now I'm trying to simply select some data from the server and display the first name in the Msgbox (or anything for that matter). Here is the code I've gotten so far....

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
    Public conn As New SqlConnection("data source=Tuys1;initial catalog=DDDBuyer; user ID=userID; password=password")
    Private dA As New SqlDataAdapter("Select FirstName, LastName from tblBuyers where Buyerid=1473", conn)
    Private dS As New DataSet
End Class

I'm not really sure how to go from here... I understand there is something like

.hasRows

if ds.hasrows then
   msgbox = "HELLO"
End if

or something like displaying the FirstName in txtFirstName

I am trying to get an idea of how ths works and after some research it's hard to find a specific example that would help me do what I'm trying to accomplish.

更新时间:2022-09-24 07:09

最满意答案

Dim custAdapter As SqlDataAdapter = New SqlDataAdapter( _
  "SELECT * FROM dbo.Customers", customerConnection)

Dim ordAdapter As OleDbDataAdapter = New OleDbDataAdapter( _
  "SELECT * FROM Orders", orderConnection)

Dim customerOrders As DataSet = New DataSet()
custAdapter.Fill(customerOrders, "Customers")
ordAdapter.Fill(customerOrders, "Orders")

Dim relation As DataRelation = _
  customerOrders.Relations.Add("CustOrders", _
  customerOrders.Tables("Customers").Columns("CustomerID"), _ 
  customerOrders.Tables("Orders").Columns("CustomerID"))

Dim pRow, cRow As DataRow
For Each pRow In customerOrders.Tables("Customers").Rows
  Console.WriteLine(pRow("CustomerID").ToString())

  For Each cRow In pRow.GetChildRows(relation)
    Console.WriteLine(vbTab & cRow("OrderID").ToString())
  Next
Next 

如此处所述


Dim custAdapter As SqlDataAdapter = New SqlDataAdapter( _
  "SELECT * FROM dbo.Customers", customerConnection)

Dim ordAdapter As OleDbDataAdapter = New OleDbDataAdapter( _
  "SELECT * FROM Orders", orderConnection)

Dim customerOrders As DataSet = New DataSet()
custAdapter.Fill(customerOrders, "Customers")
ordAdapter.Fill(customerOrders, "Orders")

Dim relation As DataRelation = _
  customerOrders.Relations.Add("CustOrders", _
  customerOrders.Tables("Customers").Columns("CustomerID"), _ 
  customerOrders.Tables("Orders").Columns("CustomerID"))

Dim pRow, cRow As DataRow
For Each pRow In customerOrders.Tables("Customers").Rows
  Console.WriteLine(pRow("CustomerID").ToString())

  For Each cRow In pRow.GetChildRows(relation)
    Console.WriteLine(vbTab & cRow("OrderID").ToString())
  Next
Next 

As explained here

相关问答

更多
  • 用于完成任务的命令,适配器或阅读器需要使用开放连接。 您已打开连接,但没有将其与MySqlCommand相关联。 因此,当您调用ExecuteReader时,它不知道如何访问您的数据库。 要解决此问题,只需使用带有sql文本和连接的MySqlCommand构造函数 Using connection = New MySqlConnection("...."= Using cmd = New MySqlCommand("SELECT * FROM orders", connection) connect ...
  • 您可以通过尝试阅读Google.com来检查互联网连接: Public Shared Function CheckForInternetConnection() As Boolean Try Using client = New WebClient() Using stream = client.OpenRead("http://www.google.com") Return True End Using ...
  • 1)从MySQL站点获取最新的连接器 2)根据他们提供的内容,将MySql.Data.dll复制到Web应用程序的Bin文件夹中 3)将此行添加到顶部的代码隐藏中: Imports MySql.Data.MySqlClient 4)使用此代码,更新连接字符串的全部大写部分: Using Con As New MySqlConnection("Database=DB;Server=SERVER-NAME;User Id=USERNAME;Password=PASSWORD;ignore prepar ...
  • 如果您尝试连接到SQL Server您的连接字符串应如下所示: Data Source=sage2\MSSQLSERVER;Database=MATERIEL;Integrated Security=False;User Instance=False;user='" + TxtUser.Text + "';password='" + TxtPassword.Text + "'" 在服务器名称之前没有必要使用反斜杠“\”。 \ sage2 - > sage2 数据库应该是服务器上显示的数据库的名称,而不是包 ...
  • 关闭连接后,您必须致电 SqlConnection.ClearPool 要么 SqlConnection.ClearAllPools 为了从SqlServer释放文件。 After you closed the connection you must call SqlConnection.ClearPool or SqlConnection.ClearAllPools in order to release the files from the SqlServer.
  • 在尝试调用ExecuteReader之前,您不会将MysqlConn和Query与您的Command相关联。 因此,它当时没有有效的连接。 At no point do you associate your MysqlConn nor Query to your Command before trying to call ExecuteReader on it. As such, it doesn't have a valid connection at that time.
  • “Main()”是一个功能。 并且您声明了一个局部变量“sConnection”。 您的表格是另一个类。 类只能访问其成员,全局成员或全局静态成员(或某些朋友方案,如C ++)。 从“Main”中取出声明,在表单范围内声明或将其声明为全局变量,表单可以访问。 或者将您的连接字符串放在配置文件中并从中读取。 (在以后的时间点很容易配置。) The "Main()" is a function. And you declared a local variable "sConnection". Your Form ...
  • 不是我所知道的。 正如Max所说,您可以安装Express版本并在本地试用。 如果您仍想使用公共数据库进行测试,请在shobankr [at] gmail.com删除平均电子邮件..我有一个公共数据库,我用它来测试我的应用程序。 Not one I am aware of. As Max said you can install Express edition and try it out locally. If you still want to test with a public DB then dr ...
  • Dim custAdapter As SqlDataAdapter = New SqlDataAdapter( _ "SELECT * FROM dbo.Customers", customerConnection) Dim ordAdapter As OleDbDataAdapter = New OleDbDataAdapter( _ "SELECT * FROM Orders", orderConnection) Dim customerOrders As DataSet = New Dat ...
  • 关于DataAdapter.Update的作用,你有点困惑。 Update可以将InsertCommand,UpdateCommand,DeleteCommand应用于DataSource中存在的每个已添加,已修改或已删除的行(假设ds是一个DataSet)。 它不会自己添加/删除/更新记录到数据库。 如果要将记录添加到数据库,则应编写(伪)代码 Using con = GetConnection() Using cmd = new con.CreateCommand() cmd.CommandTe ...

相关文章

更多

最新问答

更多
  • 在javascript中创建类以创建对象并在Java中创建类和对象之间的区别(Difference between creating a class in javascript to create an object and creating an class and object in Java)
  • Facebook API:将身份验证详细信息从Javascript SDK发送到PHP SDK(Facebook API: Send authentication detail from Javascript SDK to PHP SDK)
  • 如何停止队列动画jquery?(How can I stop queue animation jquery?)
  • 使用C#的井字游戏中的人工智能(Artificial Intelligence in Tic-Tac-Toe using C#)
  • 多少流量可以共享虚拟主机(对于Python Django站点)支持?(How Much Traffic Can Shared Web Hosting (for a Python Django site) support?)
  • 带有CIFilters的CAShapeLayer(CAShapeLayer with CIFilters)
  • 如何在Angular 2中读取JSON #text(How to read in Angular 2 the JSON #text)
  • 如何在xml中读取自闭标签的属性?(How to read self closing tag's attribute in xml?)
  • 无法使用http put将图像上传到亚马逊S3(Cannot upload image to amazon s3 using http put)
  • 文件结束无限循环(end of file infinite while-loop)
  • 在cpp的模板(template in cpp)
  • 在构建库时,clang和clang ++有什么区别?(What's the difference between clang and clang++ when building a library?)
  • ng类中的表达式(expression inside ng-class)
  • 在PHP中获取随机布尔值true / false(Get random boolean true/false in PHP)
  • 管道的高效分块用于严格的字节串(Efficient chunking of conduit for strict bytestring)
  • Python ternary_operator(如果其他标志做了其他操作,则执行其他操作)(Python ternary_operator (do-somthing if flag else do-another))
  • Sencha Touch面具发布(Sencha Touch mask ondisclosure)
  • 验证脚本上的通知[重复](Notices on validation script [duplicate])
  • 朋友功能(friend function)
  • 基于角坐标平移和变换平面几何(Translate and transform plane geometry based on corner coordinates)
  • Rails:'如果在本地运行'条件javascript标记包括(Rails: 'if running locally' conditional javascript tag include)
  • 解压文件(Unzipping files)
  • 使用ui-router以角度加载变量状态(loading in variable states with ui-router in angular)
  • 创建Azure云服务需要多长时间?(how long does it take to create an Azure Cloud Service? How to view log information?)
  • 指向整数的指针数组(Array of pointers to integers)
  • Laravel服务提供商没有看到我的包的主要类(Laravel service provider does not see the main class of my package)
  • 这个关于VSS / RSS / PSS / USS的解释是否准确?(Is this explanation about VSS/RSS/PSS/USS accurate?)
  • 在Django-Admin中通过row-id排序显示项目(Ordering the display items by row-id in Django-Admin)
  • 如何使用cythonize启用`--embed`?(How to enable `--embed` with cythonize?)
  • 用于将文本多行设置的Excel脚本(Excel script for ereasing text multiple rows)