我需要帮助连接到使用vb.net访问

时间:2016-09-07 08:17:18

标签: vb.net ms-access

我的程序不使用我想要的连接字符串。相反,它会在此文件夹中找到访问文件并显示错误:

  

找不到文件'C:\ Users \ user \ Documents \ Visual Studio   2008 \项目\ Patientt \ Patientt \ BIN \调试\ db_hospital.accdb”。

这是我的代码

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

        Provider = "Provider=Microsoft.ACE.oleDB.12.0;data source=" '& System.Environment.CurrentDirectory.ToString() & "\namu.mdb"
        datafile = "\DemsV.accdb"

        connString = Provider & datafile
        Try
            myConnection.ConnectionString = connString
            myConnection.Open()

            Dim str As String
            str = "insert into Member([MemberID],[Name],[Surname],[Date of Birth],[Contacts],[Gender],) Values (?,?,?,?,?,?)"
            Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
            cmd.Parameters.Add(New OleDbParameter("MemberID", CType(txtmemberid.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("Name", CType(txtname.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("Surname", CType(txtsurname.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("Date of Birth ", CType(DateTimePicker1.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("Contacts", CType(txtcontacts.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("Gender", CType(comGender.Text, String)))


        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

    End Sub

2 个答案:

答案 0 :(得分:0)

你有没有尝试过这方面的事情?

  Try
        Dim dbProvider As String
        Dim dbSource As String

        mainDBconnection = New OleDb.OleDbConnection

        dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
        dbSource = ' Path to your database

        mainDBconnection.ConnectionString = dbProvider & dbSource
        mainDBconnection.Open()

    Catch ex As Exception
    End Try

答案 1 :(得分:0)

它基本上都是这样做的。

Imports System.Data.OleDb

Public Class Form1



    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        ' Requires: Imports System.Data.OleDb

        ' ensures the connection is closed and disposed
        Using connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=""C:\your_path_here\InsertInto.mdb"";" & _
            "Persist Security Info=False")
            ' open connection
            connection.Open()

            ' Create command
            Dim insertCommand As New OleDbCommand( _
                "INSERT INTO Table1([inputOne] , [inputTwo] , [inputThree]) " & _
                "VALUES (@inputOne, @inputTwo, @inputThree);", _
                connection)
            ' Add the parameters with value
            insertCommand.Parameters.AddWithValue("@inputOne", TextBox1.Text)
            insertCommand.Parameters.AddWithValue("@inputTwo", TextBox2.Text)
            insertCommand.Parameters.AddWithValue("@inputThree", TextBox3.Text)
            ' you should always use parameterized queries to avoid SQL Injection
            ' execute the command
            insertCommand.ExecuteNonQuery()

            MessageBox.Show("Insert is done!!")

        End Using

    End Sub
End Class

您能否根据您的具体需求进行调整?