如何在VB.NET 2008中创建和恢复访问数据库 [英] HOW TO CREATE AND RESTORE ACCESS DATABASE WITHIN VB.NET 2008

查看:53
本文介绍了如何在VB.NET 2008中创建和恢复访问数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生,

我已经开发了一个带有vb.net 2008的软件,而在后端Ms Access 2003我的项目几乎已经完成但是缺乏我的项目没有选项来创建和恢复访问数据库以避免任何不便。

I have developed a software with vb.net 2008 and in the back end Ms Access 2003 my project has nearly been completed but there is an lack in my project that there is no option to create and restore access database in order to secure from any inconvinience.

plz帮助我如何在vb.net中创建和恢复访问数据库。 如果有人有源代码plz发送给我它会非常感兴趣

plz help me how to create and restore access database in vb.net.  if anyone have source code plz send me it will be greatly appriciated

推荐答案

你好,

可以使用代码创建MS-Access数据库,或者在应用程序文件夹的子文件夹中存储新的MS-Access数据库。如果您担心有人可能会弄乱新副本,那么使用
以外的扩展名存储数据库文件的默认扩展名。当需要新数据库将文件从当前位置复制到生产文件夹时,如果您更改了刚刚描述的扩展名,也要重命名该文件。

It is possible to create an MS-Access database with code or store a fresh MS-Access database in a sub folder of your application folder. If you are concern that someone might mess with the fresh copy then store the database file with an extension other than the default extension. When there is a need for the fresh database copy the file from the current location to the production folder and rename the file also if you had changed the extension as just described.

如果您真的想要创建一个新数据库,然后为
Microsoft ADO Ext 6.0 for DLL and Security 添加对项目的引用。以下代码使用ADO创建MS-Access新数据库,然后使用SQL创建表,填充几行。如果您的数据库不是很复杂,那么只需按照下面的模式进行操作即使
如果您的数据库模式很复杂,您也可以考虑将备用文件从一个位置复制到生产文件夹中。

If you truly want to create a new database then add a reference to your project for Microsoft ADO Ext 6.0 for DLL and Security. The following code creates a MS-Access new database using ADO followed by creating a table using SQL, populates a few rows. If your database is not very complex then simply follow the pattern below although if you database schema is complex you might want to consider the alternative as in copying a fresh database file from one location to the production folder.

请注意,代码使用从Framework 3.5,Visual Studio 2008开始的功能。

Please note the code makes use of features beginning with Framework 3.5, Visual Studio 2008.


Public Class Form1
    Private adoxFileName As String = _ 
       Application.StartupPath & "\AdoxDatabase.mdb"
    Private ConnectionString As String = _ 
       String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", adoxFileName)

    Sub CreateNewAccessDatabase(ByVal FileName As String)
        ' The following will show up as an error until you build the project.
        Dim dbCatalog As New ADOX.Catalog()
        dbCatalog.Create(ConnectionString)
    End Sub
    Private Sub cmdCreateUsingADOX_Click( _ 
        ByVal sender As System.Object, _ 
        ByVal e As System.EventArgs) Handles cmdCreateUsingADOX.Click

        If Not IO.File.Exists(adoxFileName) Then
            CreateNewAccessDatabase(adoxFileName)
        End If

        Using cn As New OleDb.OleDbConnection(ConnectionString)
            Dim cmd As New OleDb.OleDbCommand( _
            <text>
                CREATE TABLE persons ([Identifier] int identity ,
                                      UserName NVarchar(50), 
                                      [Password] NVarchar(50),
                                      CONSTRAINT [pk_AutoId] PRIMARY KEY ([Identifier])) 
            </text>.Value, cn)

            cn.Open()

            Try
                cmd.ExecuteNonQuery()
            Catch ex As OleDb.OleDbException
                MessageBox.Show(ex.Message, "OleDbException")
                Exit Sub
            Catch ex As Exception
                MessageBox.Show(ex.Message, "GeneralException")
                Exit Sub
            End Try

            Application.DoEvents()

            cmd.CommandText = _
            <Text>
                INSERT INTO persons 
                (UserName,[Password])
                VALUES 
                ("Gallagher","pass1word")
            </Text>.Value
            cmd.ExecuteNonQuery()

            cmd.CommandText = _
            <Text>
                INSERT INTO persons 
                (UserName,[Password])
                VALUES 
                ("Jones","Fluffy2")
            </Text>.Value
            cmd.ExecuteNonQuery()

            cmd.CommandText = _
            <Text>
                INSERT INTO persons 
                (UserName,[Password])
                VALUES 
                ("Smith","abv123w")
            </Text>.Value
            cmd.ExecuteNonQuery()

            cmd.CommandText = _
            <Text>
                INSERT INTO persons 
                (UserName,[Password])
                VALUES 
                ("Student1","College")
            </Text>.Value
            cmd.ExecuteNonQuery()

            cmd.CommandText = _
            <Text>
                INSERT INTO persons 
                (UserName,[Password])
                VALUES 
                ("HallBrook","1234")
            </Text>.Value
            cmd.ExecuteNonQuery()

            cmd.CommandText = _
            <Text>
                INSERT INTO persons 
                (UserName,[Password])
                VALUES 
                ("Simpson","Homer1")
            </Text>.Value
            cmd.ExecuteNonQuery()

            cmd.CommandText = "SELECT * FROM Persons ORDER BY UserName"

            Dim dt As New DataTable
            dt.Load(cmd.ExecuteReader())
            Console.WriteLine("There are [{0}] rows in person table", _ 
               cn.RecordCount("Persons").ToString)
            cn.Close()
        End Using
    End Sub    
End Class    


这篇关于如何在VB.NET 2008中创建和恢复访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆