十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
1、VB.NET是一个高级程序设计语言
创新互联公司网站建设服务商,为中小企业提供网站设计、成都网站制作服务,网站设计,网站托管、服务器托管等一站式综合服务型公司,专业打造企业形象网站,让您在众多竞争对手中脱颖而出创新互联公司。
2、ACCESS是一个办公型的数据库
3、只要设置正确、代码完整是完全可以操作的
Imports System.Data.OleDb
'Imports System.Data.OracleClient
Imports System.Data.SqlClient
'链接Oracle
'cn = New SqlConnection(""Data Source=TNS名称;Persist Security Info=True;User ID=用户名;Password=密码;" ")
'Imports System.Data.OracleClient
'Dim oOracleConn As OracleConnection = New OracleConnection()
'oOracleConn.ConnectionString = ""Data Source=MyOracleDB; User Id=username; Password=passwd; Integrated Security=no; "
'oOracleConn.Open()
'链接SQL Server
'cn = New SqlConnection("Data Source=.;Initial Catalog=Sales;Integrated Security=False;User ID=sa;Password=;")
'链接Access
'cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" db_path ";Persist Security Info=False")
Public Class Fun_DataBase
Dim cn As OleDbConnection
Dim cm As OleDbCommand
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim db_path As String = "D:\Documents\Visual Studio 2013\Projects\VBnet1\VBnet1\Resources\Database1.mdb"
Function Fun_sql1(Str_sql As String, V_调用来源 As String) '按指定sql,反馈1个查询值
Try
'Dim cn As New OleDbConnection("Data Source=.;Initial Catalog=Sales;Integrated Security=False;User ID=sa;Password=123456;")
'链接Access
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" db_path ";Persist Security Info=False")
cn.Open() 'sql语句执行前,必须打开连接
cm = New OleDbCommand(Str_sql, cn)
cm.ExecuteNonQuery()
Dim strValue As String = cm.ExecuteScalar.ToString
'MessageBox.Show(strValue)
Fun_sql1 = strValue
cn.Close()
Fun_sql1 = True
Catch ex As Exception
MessageBox.Show(V_调用来源 ":执行sql语句失败!" Str_sql)
Fun_sql1 = False
End Try
End Function
Function Fun_sqln(Str_sql As String, V_调用来源 As String) '按指定sql,反馈1行n列查询值
Try
'链接Access
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" db_path ";Persist Security Info=False")
cn.Open() 'sql语句执行前,必须打开连接
Dim cmd As New OleDbCommand("select * from grade", cn)
'Dim dr As SqlDataReader
Dim dr As OleDbDataReader
dr = cmd.ExecuteReader()
Dim strDisplay As String = ""
While dr.Read()
Fun_sqln = dr(0).ToString "|"
End While
cn.Close()
Fun_sqln = True
Catch ex As Exception
MessageBox.Show(V_调用来源 ":执行sql语句失败!" Str_sql)
Fun_sqln = False
End Try
'Dim s = Split(F.Fun_sqln("ef"), "|")
'Dim i As Integer
'i = UBound(s)
'For i = 0 To UBound(s)
' MessageBox.Show(s(i))
'Next
End Function
Public Function Fun_sql_exec(Str_sql As String, V_调用来源 As String) '执行指定sql,反馈T|F
Try
'链接Access
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" db_path ";Persist Security Info=False")
cn.Open() 'sql语句执行前,必须打开连接
cm = New OleDbCommand(Str_sql, cn)
cm.ExecuteNonQuery()
cn.Close()
Fun_sql_exec = True
Catch ex As Exception
MessageBox.Show(V_调用来源 ":执行sql语句失败!" Str_sql)
Fun_sql_exec = False
End Try
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
sQueryString是SQL(增删查改)语句
public
Boolean
ExecSQL(string
sQueryString)
{
SqlConnection
con
=
new
SqlConnection(ConfigurationManager.AppSettings["conStr"]);
con.Open();
SqlCommand
dbCommand
=
new
SqlCommand(sQueryString,
con);
try
{
dbCommand.ExecuteNonQuery();
con.Close();
}
catch
{
con.Close();
return
false;
}
return
true;
}
}
建立一个类,明自己取,然后调用就是了
例如:
类
BC=NEW
类();
STRING
ist="INSERT
INTO
XX(1,2,3)VALUES(A,B,C)";
BC.ExecSQL(ist);
就可以了
见下面的代码,自己换成真实的数据库及表即可
Imports System.Data
Imports System.IO
Imports System.Data.OleDb
Module Module1
Public cn1 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ACCESS数据库1.mdb") '定义连接1---这里请更改为实际数据库路径及名称
Public cn2 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ACCESS数据库2.mdb") '定义连接2---这里请更改为实际数据库路径及名称
Public DataBaseRST1 As Integer '用来返回数据库执行结果
Public DataBaseRST2 As Integer '用来返回数据库执行结果
Public Function DataModify(ByVal str_cmd1 As String, ByVal str_cmd2 As String) As Boolean '进行数据库修改操作函数
Dim cmdinsert1 As New OleDbCommand
Dim cmdinsert2 As New OleDbCommand
Try
cmdinsert1.CommandText = str_cmd1
cmdinsert2.CommandText = str_cmd2
cmdinsert1.Connection = cn1
cmdinsert2.Connection = cn2
If cn1.State = ConnectionState.Closed Then cn1.Open()
If cn2.State = ConnectionState.Closed Then cn2.Open()
DataBaseRST1 = cmdinsert1.ExecuteNonQuery() '用来返回执行的结果
DataBaseRST2 = cmdinsert2.ExecuteNonQuery() '用来返回执行的结果
cn1.Close()
cn2.Close()
Return True
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
End Module
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DataModify("insert into aa values ('1','2')", "insert into aa values ('1','2')") '调用方法
End Sub
End Class