vb进行数据库程序时可以在代码中使用SQL吗?方法呢?
1个回答
展开全部
可以的,具体是:
VB6连接Access
'如无法声明 Database ,就要先进行加载。
Public G_datZGSJ As Database
Set G_datZGSJ = OpenDatabase(Gp_LuJing & "edit.mdb", False, False, ";UID=;PWD=")
'打开设置表
Dim B_r1 As Recordset
Set B_r1 = G_datEdit.OpenRecordset("select * from 设置 order by 名称", dbOpenDynaset)
If B_r1.RecordCount > 0 Then'看是否有值
B_r1.MoveLast'移动
textbox1.text=B_r1![工资编号]'取值 [工资编号]是数据库中的字段名称。
'进行修改
B_r1.edit
B_r1![工资编号]="0111"
B_r1.Update
'修改完成
'进行删除
B_r1.Delete
'进行增加
'记录位置位于 Recordset 对象的第一个记录之前
If B_r1.BOF = True Then
B_r1.AddNew
B_r1![工资编号] = bxLei.Kong![工资编号]
B_r1![姓名] = bxLei.Kong![姓名]
B_r1![开户行] = bxLei.Kong![q2]
B_r1![工号] = bxLei.Kong![工号]
B_r1.Update
End If
end if
'上述的[]里的内容是数据表中的字段名称。
vb.net进行连接Access的方法:
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& "D:\web\clgl\db1.mdb;User Id=admin;Password=;")
Dim re As New DataSet
Dim a As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("select top 1 * from ipjilu", conn)
conn.Open()
Dim b As New OleDb.OleDbCommandBuilder
b.DataAdapter = a
a.Fill(re, "b")
'(访问统计库中增加一行)写入出访IP及时间
Dim bxrow As DataRow
bxrow = re.Tables("b").NewRow
bxrow("ip") = Request.ServerVariables("REMOTE_ADDR")
bxrow("shijian") = Date.Now.Now.ToString
re.Tables("b").Rows.Add(bxrow)
a.Update(re, "b")
conn.Close()
'以上增加了一行数据
'读
Dim re As New DataSet
Dim a As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("select top 1 * from ipjilu", conn)
conn.Open()
Dim b As New OleDb.OleDbCommandBuilder
b.DataAdapter = a
a.Fill(re, "b")
Return re.Tables("b").row(0)(0).tostring
'vb.net 连接sql server
Public Conn As New SqlClient.SqlConnection '数据库
Connstr="data source=192.0.0.1;initial catalog=数据库名;user id=sa;password=;"
Conn.ConnectionString = Connstr
'读数据,指定一个sql的语句,返回一个DataTable
Public Function GetDataTable(ByVal sqls As String) As DataTable
If Conn.State = 0 Then
Conn.Open()
End If
Dim bxcmd As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(sqls, Conn)
Dim bxre As New DataSet
bxcmd.Fill(bxre, "b")
Return bxre.Tables("b")
End Function
VB6连接Access
'如无法声明 Database ,就要先进行加载。
Public G_datZGSJ As Database
Set G_datZGSJ = OpenDatabase(Gp_LuJing & "edit.mdb", False, False, ";UID=;PWD=")
'打开设置表
Dim B_r1 As Recordset
Set B_r1 = G_datEdit.OpenRecordset("select * from 设置 order by 名称", dbOpenDynaset)
If B_r1.RecordCount > 0 Then'看是否有值
B_r1.MoveLast'移动
textbox1.text=B_r1![工资编号]'取值 [工资编号]是数据库中的字段名称。
'进行修改
B_r1.edit
B_r1![工资编号]="0111"
B_r1.Update
'修改完成
'进行删除
B_r1.Delete
'进行增加
'记录位置位于 Recordset 对象的第一个记录之前
If B_r1.BOF = True Then
B_r1.AddNew
B_r1![工资编号] = bxLei.Kong![工资编号]
B_r1![姓名] = bxLei.Kong![姓名]
B_r1![开户行] = bxLei.Kong![q2]
B_r1![工号] = bxLei.Kong![工号]
B_r1.Update
End If
end if
'上述的[]里的内容是数据表中的字段名称。
vb.net进行连接Access的方法:
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& "D:\web\clgl\db1.mdb;User Id=admin;Password=;")
Dim re As New DataSet
Dim a As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("select top 1 * from ipjilu", conn)
conn.Open()
Dim b As New OleDb.OleDbCommandBuilder
b.DataAdapter = a
a.Fill(re, "b")
'(访问统计库中增加一行)写入出访IP及时间
Dim bxrow As DataRow
bxrow = re.Tables("b").NewRow
bxrow("ip") = Request.ServerVariables("REMOTE_ADDR")
bxrow("shijian") = Date.Now.Now.ToString
re.Tables("b").Rows.Add(bxrow)
a.Update(re, "b")
conn.Close()
'以上增加了一行数据
'读
Dim re As New DataSet
Dim a As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("select top 1 * from ipjilu", conn)
conn.Open()
Dim b As New OleDb.OleDbCommandBuilder
b.DataAdapter = a
a.Fill(re, "b")
Return re.Tables("b").row(0)(0).tostring
'vb.net 连接sql server
Public Conn As New SqlClient.SqlConnection '数据库
Connstr="data source=192.0.0.1;initial catalog=数据库名;user id=sa;password=;"
Conn.ConnectionString = Connstr
'读数据,指定一个sql的语句,返回一个DataTable
Public Function GetDataTable(ByVal sqls As String) As DataTable
If Conn.State = 0 Then
Conn.Open()
End If
Dim bxcmd As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(sqls, Conn)
Dim bxre As New DataSet
bxcmd.Fill(bxre, "b")
Return bxre.Tables("b")
End Function
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询