2014-10-08 229 views
0

我試圖把這段代碼放在一個文本框中,它似乎是正確的。但是,「OldDbConnection」和「.Open」以紅色下劃線。我錯過了什麼或做錯了什麼?MS Visual Studio 2010數據庫連接到MS Access 2007

OleDbConnection conn = new 
    OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb; 
Persist Security Info=False;"); 

    try 
    { 
     conn.Open(); 
     MessageBox.Show("Connected"); 


    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
+0

你有適當的命名空間嗎? – 2014-10-08 23:51:20

+0

@ DanielA.White實際上這是出現的錯誤。 – CaptainBadass 2014-10-08 23:51:55

+0

嘗試搜索錯誤消息,看看你能找到什麼!? – 2014-10-08 23:55:34

回答

1

添加一個using System.Data.OleDb到您的文件的頂部。

0
Imports System.Data.OleDb 
Imports System.Data.OleDb.OleDbDataReader 

Public Class Form1 
    Dim provider As String 
    Dim dataFile As String 
    Dim connString As String 
    Dim myConnection As OleDbConnection = New OleDbConnection 





    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     provider = "Provider =Microsoft.ACE.OLEDB.12.0;Data Source=" 
     dataFile = "C:\Users\ANNALIZA\Desktop\New folder (4)\JABOYY1.accdb" 
     connString = provider & dataFile 
     myConnection.ConnectionString = connString 
     myConnection.Open() 


     Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [ADMIN] WHERE [userName] = '" & TextBox1.Text & "' AND [userPassword] = '" & TextBox2.Text & "'", myConnection) 
     Dim userFound As Boolean = False 
     Dim FirstName As String = "" 
     Dim LastName As String = "" 
     Dim dr As OleDbDataReader = cmd.ExecuteReader 




     While dr.Read 
      userFound = True 
      FirstName = dr("FirstName").ToString 
      LastName = dr("LastName").ToString 

     End While 
     If userFound = True Then 
      Form2.Show() 
      Form2.Label1.Text = "Welcome" & FirstName & " " & LastName 
     ElseIf TextBox1.Text = "" And TextBox2.Text = "" Then 
      MsgBox("No such username and password found! Input first before you click LOG IN!", MsgBoxStyle.Critical, "Warning") 
      Me.Show() 



     ElseIf MsgBox("Sorry username and password not found!LOGIN FAILED", MsgBoxStyle.Exclamation, "Invalid Log in") Then 
      TextBox1.Text = "" 
      TextBox2.Text = "" 
      Me.Show() 

     End If 




    End Sub 
+0

添加頂部 進口System.Data.OleDb 進口System.Data.OleDb.OleDbDataReader 公共類Form1中 昏暗的供應商作爲字符串 昏暗的數據文件作爲字符串 昏暗CONNSTRING作爲字符串 昏暗MyConnection的作爲的OleDbConnection =新的OleDbConnection – Jhayboy 2016-03-30 04:29:08