如果楼主熟悉VB6,可以直接在项目中添加ADODB的Com引用,这样你就可以像VB6那样操作数据库了!
成都创新互联-专业网站定制、快速模板网站建设、高性价比北林网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式北林网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖北林地区。费用合理售后完善,十载实体公司更值得信赖。
另外
.NET
Framework中连接数据库要用到ADO.NET。如果要操作Access数据库,要用到System.Data.OleDb命名空间下的许多类。
比如按楼主所说,“我想在textbox1中显示表一中【一些数据】字段下的第一个内容”:
'首先导入命名空间
Imports
System.Data
Imports
System.Data.OleDb
'然后在某一个事件处理程序中写:
Dim
conn
As
New
OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=数据库.accdb;Jet
OLEDB:Database
Password=MyDbPassword")
Dim
command
As
New
OleDbCommand("Select
*
From
数据表",
conn)
conn.Open()
'打开数据库连接
Dim
reader
As
OleDbDataReader
=
command.ExecuteReader()
'执行SQL语句,返回OleDbDataReader
对象
Do
While
reader.Read()
'读取一条数据
textbox1.Text
+=
reader("一些数据")
VbCrLf
Loop
reader.Close()
'关闭OleDbDataReader
conn.Close()
'关闭连接
Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click 备份数据库 BACKUP DATABASE Test TO DISK= C:\Backup WITH Format Dim cn As SqlConnection Dim cm As SqlCommand cn = getDB() cm = New SqlCommand( BACKUP DATABASE JXbase TO DISK= C:\Backup WITH Format cn) cn Open() cm ExecuteNonQuery() MsgBox( 备份成功 ) cn Close()
End Sub
恢复 Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click Dim cn As New SqlConnection Dim cn As New SqlConnection Dim mydr As SqlDataReader Dim str As String
Dim tmpConnectionString As String = Data Source=JJK;Initial Catalog=master;Integrated Security=True cn ConnectionString = tmpConnectionString cn ConnectionString = tmpConnectionString cn Open() cn Open() Dim cm As SqlCommand = New SqlCommand( select spid from master sysprocesses where dbid=db_id( JXbase ) cn) mydr = cm ExecuteReader()
Dim cm As SqlCommand = New SqlCommand() cm Connection = cn
While (mydr Read()) 杀死进程
str = kill mydr( spid ) ToString() cm CommandText = str cm CommandType = CommandType Text Application DoEvents() cm ExecuteNonQuery() 杀进程 End While mydr Close() cm = New SqlCommand( RESTORE DATABASE JXbase FROM DISK= C:\Backup cn) 恢复备份 cm ExecuteNonQuery() MsgBox( 恢复成功成功 ) cn Close() cn Close()
lishixinzhi/Article/program/net/201311/12901
1、首先打开Visual Studio 2008代码窗口,添加引用。
2、输入以下代码:Public conn1 As SqlConnection = New SqlConnection 。
3、声明关键字 Public;(因为是全局变量,所以用Public 来声明)。
4、如果SQL 数据库就在本机,则用以下代码连接。
5、如果代码太长,影响可读性,可以用空格加"_"后,回车换行即可。