连接到数据库 [英] connect to the database

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

问题描述

任何人都可以建议我如何将数据库连接为

please can anybody suggest me how can I connect database to the form

推荐答案

导航给定的链接,以了解您如何逐步连接到数据库

如何通过VB.net连接数据库 [
Navigate the given link to learn how u will connect to database step by step

How to Connect Database through VB.net[^]


您好取决于您将要连接哪个数据库,但这是连接到sql的示例代码:

hello depending on which databse you wona connect , but this is a sample code for connecting to sql :

Imports System.Data.SqlClient
Public Class Form1 Inherits System.Windows.Forms.Form
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim dr As New SqlDataReader()
''declaring the objects
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Load
myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
''establishing connection. you need to provide password for sql server
Try
myConnection.Open()
''opening the connection
myCommand = New SqlCommand("Select * from discounts", myConnection)
''executing the command and assigning it to connection 
dr = myCommand.ExecuteReader()
While dr.Read()
''reading from the datareader
MessageBox.Show("discounttype" & dr(0).ToString())
MessageBox.Show("stor_id" & dr(1).ToString())
MessageBox.Show("lowqty" & dr(2).ToString())
MessageBox.Show("highqty" & dr(3).ToString())
MessageBox.Show("discount" & dr(4).ToString())
''displaying the data from the table
End While
dr.Close()
myConnection.Close()
Catch e As Exception
End Try
End Sub
End Class  

The above code displays records from discounts table in MessageBoxes. 
Retrieving records with a Console Application 
Imports System.Data.SqlClient
Imports System.Console
Module Module1
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim dr As SqlDataReader
Sub Main()
Try
myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
''you need to provide password for sql server
myConnection.Open()
myCommand = New SqlCommand("Select * from discounts", myConnection)
dr = myCommand.ExecuteReader
Do
While dr.Read()
WriteLine(dr(0))
WriteLine(dr(1))
WriteLine(dr(2))
WriteLine(dr(3))
WriteLine(dr(4))
'' writing to console
End While
Loop While dr.NextResult()
Catch
End Try
dr.Close()
myConnection.Close()
End Sub
End Module  



还有更多详细信息:
单击此处 [



and here are more details :
Click here[^]

regards...


这篇关于连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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