sql与vb.net连接的简单介绍
vb.net 连接sql数据库模块,如何使用?
比如有个asp.net(vb.net)连接sql server 2000数据库的连接模块(module):
成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站制作、成都网站制作、仙游网络推广、小程序制作、仙游网络营销、仙游企业策划、仙游品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供仙游建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
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怎么连接SQL?
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
vb.net怎么连接SQL数据库
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
vb.net是怎么样实现与sql的连接的?
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;")
vb.net连接sql数据库
您好:给你个Sql Servers 2000的链接方法参考一下。
Public Shared connectionString As String = “Data Source=SERVERS;Initial Catalog=HL;User ID=sa;Password=pw" providerName="System.Data.SqlClient”
'summary
'执行SQL语句,返回影响的记录数
'/summary
'param name="SQLString"SQL语句/param
'returns影响的记录数/returns
Public Shared Function ExecuteSql(ByVal SQLString As String) As Integer
Using connection As New SqlConnection(connectionString)
Using cmd As New SqlCommand(SQLString, connection)
Try
connection.Open()
Dim rows As Integer = cmd.ExecuteNonQuery()
Return rows
Catch e As System.Data.SqlClient.SqlException
connection.Close()
Throw e
End Try
End Using
End Using
End Function
希望对您有用!
vb.net 中如何使用SQL语句查询数据库中的数据
1、首先打开Visual Studio 2008代码窗口,添加引用。
2、输入以下代码:Public conn1 As SqlConnection = New SqlConnection 。
3、声明关键字 Public;(因为是全局变量,所以用Public 来声明)。
4、如果SQL 数据库就在本机,则用以下代码连接。
5、如果代码太长,影响可读性,可以用空格加"_"后,回车换行即可。
标题名称:sql与vb.net连接的简单介绍
转载来于:http://cdiso.cn/article/hicjjc.html