十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Option Explicit On
10年积累的成都网站建设、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有黎平免费网站建设让你可以放心的选择与我们合作。
Option Strict On
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Program
Public Shared Sub Main()
Dim connectionString As String = _
"Data Source=(local);Initial Catalog=Northwind;" _
"Integrated Security=true"
' Provide the query string with a parameter placeholder.
Dim queryString As String = _
"SELECT ProductID, UnitPrice, ProductName from dbo.Products " _
"WHERE UnitPrice @pricePoint " _
"ORDER BY UnitPrice DESC;"
' Specify the parameter value.
Dim paramValue As Integer = 5
' Create and open the connection in a using block. This
' ensures that all resources will be closed and disposed
' when the code exits.
Using connection As New SqlConnection(connectionString)
' Create the Command and Parameter objects.
Dim command As New SqlCommand(queryString, connection)
command.Parameters.AddWithValue("@pricePoint", paramValue)
' Open the connection in a try/catch block.
' Create and execute the DataReader, writing the result
' set to the console window.
Try
connection.Open()
Dim dataReader As SqlDataReader = _
command.ExecuteReader()
Do While dataReader.Read()
Console.WriteLine( _
vbTab "{0}" vbTab "{1}" vbTab "{2}", _
dataReader(0), dataReader(1), dataReader(2))
Loop
dataReader.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
End Using
End Sub
End Class
这是我在vs2010中微软自带的MSDN示例代码里面拷的,是关于ADO.net连接sql的操作。
希望对你有帮助。 如果你还需要其他的,我也可以再拷给你看。
Dim OraSession As Object '会话对象
Dim OraDatabase As Object '数据库对象
Dim OraDynaset As Object'动态集对象
Dim OraDatabase As Object’字段集对象
Set OraSession =CreateObject("OracleInProcServer.XOraSession")
Set OraDatabase=OraSession.CreateDatabase
("Demo","scott/tiger",0)'建立数据库连接
Set OraDynaset=OraDatabase.CreateDynaset("select*from
student",0)'初始化动态集对象
’添加记录
'Begin an AddNew
OraDynaset.DbAddNew
OraDynaset.Fields("ID").value="1000"
’ID为列名
OraDynaset.DbUpdate'更新数据到表
’删除
OraDynaset.DbDelete
'修改
OraDynaset.DbEdit
OraDynaset.Fields("ID").value="101210"
OraDynaset.DbUpdate
'数据库连接函数,其中mysql(数据集连接语句),sqlip(SQL服务器IP),rsmod(数据库打开模式)为动态取值
Public Function myRs(ByVal mySQL As String, ByVal sqlIP As String, ByVal rsMod As Integer) As ADODB.Recordset
Dim myConn As New ADODB.Connection
If myConn.State = 1 Then
myConn.Close()
End If
If rs.State = 1 Then
rs.Close()
End If
myConn.ConnectionString = "Provider = Sqloledb;SERVER=" sqlIP ";uid=sa;pwd=;database=school"
myConn.Open()
rs.Open(mySQL, myConn, 1, rsMod)
myRs = rs
End Function
'显示列表内容函数,该函数能显示两个子项
Public Function addLst() As Boolean
lstUser.Clear()
mySql = "select * from sqlusers"
rs = myRs(mySql, sqlIP, 1)
lstUser.View = View.Details
Dim i As Integer
If rs.Fields.Count 0 Then
'添加列表框列标题项目
For i = 1 To rs.Fields.Count
lstUser.Columns.Add(rs.Fields.Item(i - 1).Name)
Next
'添加列表框数据内容
If rs.RecordCount 0 Then
rs.MoveFirst()
For i = 1 To rs.RecordCount
lstUser.Items.Add(rs("用户名").Value)
lstUser.Items.Item(i - 1).SubItems.Add(rs("密码").Value)
lstUser.Items.Item(i - 1).SubItems.Add(rs("用户类型").Value)
rs.MoveNext()
Next
End If
End Function
以上是我刚刚写完的,调试通过,希望对你有用.
一般都是vb.net传入参数 交给存储过程去做的 你是要在vb.net前台就执行的例子 还是想要调用存储过程的例子
前台的一般就是这样
Dim paras As New NameValueCollection
paras .Append("delete from T_AlarmConfig ")
paras .Append(" where AlarmConfigID=@AlarmConfigID")
调用存储过程的就是给写存储过程了,这是调用存储过程的例子,只能理解理解
''' summary
''' 删除数据
''' /summary
''' param name="procName"存储过程名/param
''' param name="paras"存储过程参数/param
''' remarks
''' 2006-5-29 zhuzh Created
''' /remarks
Public Sub DeleteData(ByVal procName As String, _
ByVal paras As NameValueCollection, _
Optional ByVal timeout As Integer = 30)
eventCount += 1
'存储过程参数名数组
Dim paraNames As String() = {}
'存储过程参数值数组
Dim paraValues As String() = {}
'传入的参数转化成数组
Me.ConvertNameValueCollection(paras, paraNames, paraValues, STATUS_UPDATE)
'判断系统类型调用相应模块的方法
If isWebSys Then
DeleteDelegateInstance = AddressOf webGw.ExecProc
Else
DeleteDelegateInstance = AddressOf AdoGw.ExecProc
End If
Dim ret As Integer = 0
Dim ds As New DataSet
'----guojun 2008-1-2 add-----
If timeout 60000 Then
timeout = 60000
End If
'--------防止超时异常--------
DeleteDelegateInstance.BeginInvoke(procName, ret, ds, paraNames, paraValues, timeout, _
AddressOf DataDeletedCallBack, Nothing)
End Sub
Imports System
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Inherits Form
Private components As System.ComponentModel.Container = Nothing
Private WithEvents lastrec As Button
Private WithEvents nextrec As Button
Private WithEvents previousrec As Button
Private WithEvents firstrec As Button
Private t_books As TextBox
Private t_nl As TextBox
Private t_xb As TextBox
Private t_xm As TextBox
Private t_id As TextBox
Private l_books As Label
Private l_nl As Label
Private l_xb As Label
Private l_xm As Label
Private l_id As Label
Private label1 As Label
Private myDataSet As DataSet
Private WithEvents button1 As Button
Private WithEvents button2 As Button
Private WithEvents button3 As Button
Private WithEvents button4 As Button
Private myBind As BindingManagerBase
Public Sub New ( )
MyBase.New ( )
GetConnected ( )
InitializeComponent ( )
End Sub
'清除在程序中使用过的资源
Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose ( )
End If
End If
MyBase.Dispose ( disposing )
End Sub
Public Sub GetConnected ( )
'创建一个数据连接
Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0 ;
Data Source = db.mdb "
Dim myConn As OleDbConnection = New OleDbConnection ( )
myConn.ConnectionString = strCon
Dim strCom As String = " SELECT * FROM person "
'创建一个 DataSet
myDataSet = New DataSet ( )
myConn.Open ( )
'通过OleDbDataAdapter对象得到一个数据集
Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter
( strCom , myConn )
'把Dataset绑定books数据表
myCommand.Fill ( myDataSet , "person" )
'关闭此数据连接
myConn.Close ( )
End Sub
'插入数据记录操作代码
Private Sub button2_Click (ByVal sender As Object , _
ByVal e As System.EventArgs) Handles button2.Click
'判断所有字段是否添完,添完则执行,反之弹出提示
If ( t_id.Text "" And t_xm.Text ""
And t_xb.Text "" And t_nl.Text ""
And t_books.Text "" ) Then
Dim myConn1 As String = " Provider = Microsoft.Jet.OLEDB.4.0 ;
Data Source = db.mdb"
Dim myConn As OleDbConnection = New OleDbConnection ( myConn1 )
myConn.Open ( )
Dim strInsert As String = " INSERT INTO person ( id , xm , xb , nl
, zip ) VALUES ( " _
t_id.Text + " , '" _
t_xm.Text + "' , '" _
t_xb.Text + "' , " _
t_nl.Text + " , " _
t_books.Text + ")"
Dim inst As OleDbCommand = New OleDbCommand ( strInsert , myConn )
inst.ExecuteNonQuery ( )
myConn.Close ( )
myDataSet.Tables ( "person" ).Rows ( myBind.Position ).BeginEdit ( )
myDataSet.Tables ( "person" ).Rows ( myBind.Position ).EndEdit ( )
myDataSet.Tables ( "person" ).AcceptChanges ( )
Else
MessageBox.Show ( "必须填满所有字段值!" , "错误!" )
End If
End Sub