添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
-----------------------------------------------我是分割线-------------------------------------------- 下面是详细的代码:        这一部分是有关数据访问的类,主要的操作就是把数据存入刚刚说到的表中,还有一个是把表中数据读出 Public Sub New(ByVal dbPath As String) conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\" & dbPath) End Sub Public Sub insertIntoTable(ByVal tableName As String, ByRef options As Object()) '生成SQL字符串,这个字符串可以按自己的需要自行更改,我这里是将4个字段插入数据 Dim str As String = "INSERT INTO " & tableName & " (timu,daan,jiexi,pic) VALUES(" Dim comm As OleDbCommand For i As Integer = 0 To options.Count - 1 If i < options.Count - 1 Then str = str & "?," str = str & "?)" End If '到这里str会形成:insert into 表名 values(?,?,?,?)这样的字符串,?为占位符,可以加入数据 conn.Open() comm = New OleDbCommand(str, conn) '这里的添加数据,这里我用的是OBJECT数组,由外部添加 '如果想自己添加的话, 'comm.Parameters.Add(New OleDb.OleDbParameter) 'comm.Parameters(第几个参数).Value = 值 For i As Integer = 0 To options.Count - 1 comm.Parameters.Add(New OleDb.OleDbParameter) comm.Parameters(i).Value = options(i) comm.ExecuteNonQuery() 'sql写完了就开始执行 conn.Close() MsgBox("Save OK", MsgBoxStyle.Information) '给个提示 End Sub ''' <summary> ''' 这是一个查询,最简单的Select了,把所有数据都查询出来 ''' </summary> ''' <param name="tableName"></param> ''' <returns>返回一个DataSet</returns> ''' <remarks></remarks> Public Function selectByTableName(ByVal tableName As String) As DataSet Dim str As String = "select * from " & tableName conn.Open() Dim cmd As New OleDb.OleDbCommand(str, conn) Dim RS As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(cmd) Dim DT As New DataSet RS.Fill(DT, "timu") conn.Close() Return DT End Function End Class Imports System.Data.OleDb Public Class AccessDBTools Private conn As OleDbConnection Public Sub New(ByVal dbPath As String) conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\" & dbPath) End Sub Public Sub insertIntoTable(ByVal tableName As String, ByRef options As Object()) '生成SQL字符串,这个字符串可以按自己的需要自行更改,我这里是将4个字段插入数据 Dim str As String = "INSERT INTO " & tableName & " (timu,daan,jiexi,pic) VALUES(" Dim comm As OleDbCommand For i As Integer = 0 To options.Count - 1 If i < options.Count - 1 Then str = str & "?," str = str & "?)" End If '到这里str会形成:insert into 表名 values(?,?,?,?)这样的字符串,?为占位符,可以加入数据 conn.Open() comm = New OleDbCommand(str, conn) '这里的添加数据,这里我用的是OBJECT数组,由外部添加 '如果想自己添加的话, 'comm.Parameters.Add(New OleDb.OleDbParameter) 'comm.Parameters(第几个参数).Value = 值 For i As Integer = 0 To options.Count - 1 comm.Parameters.Add(New OleDb.OleDbParameter) comm.Parameters(i).Value = options(i) comm.ExecuteNonQuery() 'sql写完了就开始执行 conn.Close() MsgBox("Save OK", MsgBoxStyle.Information) '给个提示 End Sub ''' <summary> ''' 这是一个查询,最简单的Select了,把所有数据都查询出来 ''' </summary> ''' <param name="tableName"></param> ''' <returns>返回一个DataSet</returns> ''' <remarks></remarks> Public Function selectByTableName(ByVal tableName As String) As DataSet Dim str As String = "select * from " & tableName conn.Open() Dim cmd As New OleDb.OleDbCommand(str, conn) Dim RS As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(cmd) Dim DT As New DataSet RS.Fill(DT, "timu") conn.Close() Return DT End Function End Class -----------------------------------------------我是分割线-------------------------------------------- 窗体部分比较简单,如下图
Public Class FormMain
    Private accessTool As AccessDBTools = New AccessDBTools("data.accdb")
    Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
        Dim MyStream As New System.IO.MemoryStream
        '下面的方法把PictureBox中的Image属性存入一个流中
        Me.PictureBoxPeiTu.Image.Save(MyStream, System.Drawing.Imaging.ImageFormat.Bmp)
        '声明字节数组
        Dim MyBytes(MyStream.Length) As Byte
        '把流变成数组
        MyBytes = MyStream.ToArray()
        Dim options As Object() = {RichTextBoxTiMu.Rtf, RichTextBoxDaAn.Rtf, RichTextBoxJieXi.Rtf, MyBytes}
        '调用上个代码段类中的方法存入数据库
        accessTool.insertIntoTable("timu", options)
    End Sub
    ''' <summary>
    ''' 这个是我加的功能,点节图片框可以更换图片,可跳过
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub PictureBoxPeiTu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBoxPeiTu.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            PictureBoxPeiTu.Image = Image.FromFile(OpenFileDialog1.FileName)
        End If
    End Sub
    ''' <summary>
    ''' 这个代码是输入ID来读取一行数据
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ButtonRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRead.Click
        '调用上个代码段中ACCESS类来读取一个表的数据
        Dim allData As DataSet = accessTool.selectByTableName("timu")
        '获取输入的ID
        Dim id As Integer = CInt(TextBoxID.Text)
        'ID范围判断
        If id > allData.Tables("timu").Rows.Count Then
            MsgBox("你的ID超过最大的范围了!", MsgBoxStyle.Information)
            '将一行数据的前三个赋值到相应RichTextBox中
            RichTextBoxTiMu.Rtf = allData.Tables("timu").Rows(id - 1).Item(1).ToString()
            RichTextBoxDaAn.Rtf = allData.Tables("timu").Rows(id - 1).Item(2).ToString()
            RichTextBoxJieXi.Rtf = allData.Tables("timu").Rows(id - 1).Item(3).ToString()
            '取最后一个,即图片数据
            Dim Picturebyte = allData.Tables("timu").Rows(id - 1).Item(4)
            '用这个二进制数据创建一个流
            Dim ioStream As IO.MemoryStream = New IO.MemoryStream(Picturebyte, True)
            '用流转化成BITMAP并设置到图片框中去
            PictureBoxPeiTu.Image = Bitmap.FromStream(ioStream, True)
        End If
    End Sub
End Class
                    一、准备工作    在ACCESS数据库中,将字段声明为 OLE链接 类型二、将图片框中图片转化成二进制数组的方法        Dim MyStream As New System.IO.MemoryStream        Me.PictureBoxPeiTu.Image.Save(MyStream, System.Drawing.Imaging.ImageForma
				
终于在花了一个四个小时的时间完成了数据库vb.net的合伙连接的第一步连接数据库,在这个过程遇到了问题,而且也是出现了一些刚刚接触的新的词语和观念,很新鲜,不过无论怎么样,最终也是给解决了,还是有一点点的小开心的,希望写的博客可以真正的帮到大家,让大家在其中收到一些启发! 一:建立ACCESS数据库   针对如何连接的问题,可以举一个例子:建立一个名为叫教务的access数据库,在这里存储上
首先,你需要安装适当的驱动程序来连接 Access 数据库。你可以从 Microsoft 下载并安装 Microsoft Access 数据库引擎。接下来,你可以使用以下代码连接 Access 数据库: ```vb.net Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Persist Security Info=False;") con.Open() 在上面的代码中,你需要将 `Data Source` 更改为你的 Access 数据库文件的路径。然后,你可以使用 `con` 变量来执行查询和其他数据库操作。记得在使用完毕后关闭连接: ```vb.net con.Close() 另外,你还需要引入命名空间 `System.Data.OleDb`。