大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Public sqlcon As System.Data.SqlClient.SqlConnection
成都创新互联网站建设由有经验的网站设计师、开发人员和项目经理组成的专业建站团队,负责网站视觉设计、用户体验优化、交互设计和前端开发等方面的工作,以确保网站外观精美、成都网站设计、成都网站建设易于使用并且具有良好的响应性。
Public sqladapter As System.Data.SqlClient.SqlDataAdapter
Public command As System.Data.SqlClient.SqlCommand
Public ds As System.Data.DataSet
Public dr As System.Data.DataTable
sqlcon = New System.Data.SqlClient.SqlConnection
sqlcon.ConnectionString = "Server=(local); Initial Catalog=test1 ; User ID="数据库sa用户"; Password="数据库密码""
ds = New System.Data.DataSet
dr = New System.Data.DataTable(" test2 ")
ds.Tables.Add(dr)
sqladapter = New System.Data.SqlClient.SqlDataAdapter("select * from test2 ", sqlcon)
sqlcon.Open()
sqladapter.Fill(dr)
sqlcon.Close()
界面上控件
DataGridView1.DataSource = dr
ADO.net +SQLSever(比如说你的服务器是:MYSQLSERVER,你要连的数据库是pubs)
首先引入命名空间(在public class ..之上)
imports system.data.sqlclient
⑴信任连接(不用使用用户名,密码)
Dim mycon As New SqlConnection("server=MYSQLSERVER;database=pubs;integrated security=true;")
mycon.open()
....
mycon.close()
⑵采用用户名和密码连接(假如你的数据库登陆用户名为sa,密码为:dorient):
Dim mycon As New SqlConnection("server=MYSQLSERVER;database=pubs;uid=sa;pwd=dorient;")
比如有个asp.net(vb.net)连接sql server 2000数据库的连接模块(module):
Module MdlCommon
Public txtSQL As String '存放SQL语句
Public DBSet As DataSet '查询得到的记录集
Public ErrorMsg As String '存放错误信息
Public Function ExecuteSQL(ByVal strSQL As String, ByRef errMsg As String) As DataSet
Dim cnn As SqlClient.SqlConnection
Dim cmd As New SqlClient.SqlCommand()
Dim adpt As SqlClient.SqlDataAdapter
Dim rst As New DataSet()
Dim SplitSQL() As String
errMsg = ""
Try
SplitSQL = Split(strSQL)
cnn = New SqlClient.SqlConnection("data source=(local);initial catalog=urp;user id=sa;pwd=1234")
If InStr("INSERT,DELETE,UPDATE", UCase$(SplitSQL(0))) Then
cmd.Connection = cnn
cmd.Connection.Open()
cmd.CommandText = strSQL
cmd.ExecuteNonQuery()
Else
adpt = New SqlClient.SqlDataAdapter(strSQL, cnn)
adpt.Fill(rst)
ExecuteSQL = rst
End If
Catch ex As Exception
errMsg = ex.Message
Finally
rst = Nothing
cnn = Nothing
End Try
End Function End Module
调用时在asp.net后台写上txtSQL="SELECT * FROM user";
DBSet=ExecuteSQL(txtSQL,ErrorMsg)就可以查询db返回结果了。
VB.NET中有控件可以真接接点击属性进行连接配置.
SqlConnect数据连接控件.点击ConnectionString属性进行配置就可以了.
VB.NET中没有RecordSet了.取而代之的是DataSet.
在VB.net中可以先用SqlConnection对像来连接数据库再用SqlDataAdapter对像来读取数据并填充到DataSet里.然后就可以进行数据绑定了.
例:
dim
conn
as
SqlConnection
dim
da
as
SqlDataAdapter
dim
ds
as
Dataset
set
conn
=
new
SqlConnection
conn.ConnectionString="server=serverIP;database=databasename;uid=userid;pwd=password";
conn.open
da
=
new
SqlDataAdapter
(,conn)
da.fill(ds)
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
'数据库连接对象
'错误的写法
'Dim objConnection As SqlConnection = New SqlConnection("server=(local);database=pubs;user id=sa;password=")
'正确写法
Dim objConnection As New SqlConnection("Data Source=127.0.0.1; Initial Catalog=pubs; user id=sa; password=;")
'数据适配器
Dim objDataAdapter As SqlDataAdapter = New SqlDataAdapter()
'DataSet
Dim objDataSet As DataSet = New DataSet()
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'设置查询命令属性
objDataAdapter.SelectCommand = New SqlCommand
objDataAdapter.SelectCommand.Connection = objConnection
objDataAdapter.SelectCommand.CommandText = "select au_lname,au_fname,title,price from authors join titleauthor on authors.au_id=titleauthor.au_id join titles on titleauthor.title_id=titles.title_id order by au_lname,au_fname"
objDataAdapter.SelectCommand.CommandType = CommandType.Text
'打开数据库连接
objConnection.Open()
'填充DataSet对象
objDataAdapter.Fill(objDataSet, "authors")
'关闭数据库连接
objConnection.Close()
'给DataGrid绑定数据
grdAuthorTitles.DataSource = objDataSet
grdAuthorTitles.DataMember = "authors"
'清除
objDataAdapter = Nothing
objConnection = Nothing
End Sub
End Class