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.
最满意答案
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
相关问答
更多-
用于完成任务的命令,适配器或阅读器需要使用开放连接。 您已打开连接,但没有将其与MySqlCommand相关联。 因此,当您调用ExecuteReader时,它不知道如何访问您的数据库。 要解决此问题,只需使用带有sql文本和连接的MySqlCommand构造函数 Using connection = New MySqlConnection("...."= Using cmd = New MySqlCommand("SELECT * FROM orders", connection) connect ...
-
检查互联网连接vb.net(Check Internet Connection vb.net)[2022-08-24]
您可以通过尝试阅读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 ...
-
VB.Net连接(VB.Net connection)[2022-09-24]
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 ... -
插入OLEDB VB.NET(Insert into OLEDB VB.NET)[2023-01-31]
关于DataAdapter.Update的作用,你有点困惑。 Update可以将InsertCommand,UpdateCommand,DeleteCommand应用于DataSource中存在的每个已添加,已修改或已删除的行(假设ds是一个DataSet)。 它不会自己添加/删除/更新记录到数据库。 如果要将记录添加到数据库,则应编写(伪)代码 Using con = GetConnection() Using cmd = new con.CreateCommand() cmd.CommandTe ...