1

我試圖用兩個表連接到數據庫。但是,在我嘗試登錄後,出現錯誤。錯誤表示零點沒有行。我認爲這是因爲我的連接任何幫助,將不勝感激。與OleDbConnection連接

感謝先進!

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.OleDb; 

namespace Project3 
{ 

public partial class _Default : System.Web.UI.Page 
{ 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void login_Click(object sender, EventArgs e) 
    { 
     OleDbConnection connect = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\parodeghero\\Documents\\Visual Studio 2010\\Projects\\Project3\\Project3\\App_Data\\QA.mdb;Persist Security Info=True"); 
     //set up connection string 
     OleDbCommand command = new OleDbCommand("select * from Employee where [email protected]", connect); 
     OleDbParameter param0 = new OleDbParameter("@login", OleDbType.VarChar); 

     param0.Value = employeeID.Text; 
     command.Parameters.Add(param0); 

     //middle tier to run connect 
     OleDbDataAdapter da = new OleDbDataAdapter(command); 

     DataSet dset = new DataSet(); 

     da.Fill(dset); 

     //problem line 
     if (dset.Tables[0].Rows[0]["Password"].ToString().Equals(password.Text)) 
     { 
+1

如何調用'開()'對於初學者的連接? – 2012-03-22 23:09:44

+0

謝謝!我想我需要一個嘗試和捕獲聲明我的OleDbCommand太! – asguy 2012-03-22 23:17:51

+2

我回到原來的標題。如果您對自己的問題有自己的答案,請將其作爲答案張貼並接受。請不要使用「已解決」編輯標題,也不要使用答案編輯問題。 – LarsTech 2012-03-22 23:25:48

回答

5

你需要打開連接

protected void login_Click(object sender, EventArgs e) 
    { 
     OleDbConnection connect = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\parodeghero\\Documents\\Visual Studio 2010\\Projects\\Project3\\Project3\\App_Data\\QA.mdb;Persist Security Info=True"); 
     //set up connection string 
     OleDbCommand command = new OleDbCommand("select * from Employee where [email protected]", connect); 
     OleDbParameter param0 = new OleDbParameter("@login", OleDbType.VarChar); 

     param0.Value = employeeID.Text; 
     command.Parameters.Add(param0); 

     try 
     { 
      connect.Open(); 
     }catch(Exception err){ Debug.WriteLine(err.Message);} 

     //middle tier to run connect 
     OleDbDataAdapter da = new OleDbDataAdapter(command); 

     DataSet dset = new DataSet(); 

     da.Fill(dset); 
+0

它看起來像你的方法缺少一個右括號。 ;) – fab 2017-07-24 15:47:29

3

你不需要打開連接。 OleDbDataAdapter.Fill將打開連接並關閉它,如果它發現它關閉開始。填充將連接保留在找到它的狀態。

我的確質疑你的SQL。我對OleDb的理解是它不支持SQL中的命名參數。它需要一個佔位符「?」而不是@login。您需要每個佔位符的參數,並且參數必須按照它們出現的順序添加。如果你的SQL無效,那麼你將在DataTable中有一個SQL異常或沒有數據,即NO行0!

1

OLEDB連接完整的代碼

http://csharp.net-informations.com/data-providers/csharp-oledb-connection.htm

 string connetionString = null; 
     OleDbConnection cnn ; 
     connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=yourdatabasename.mdb;"; 
     cnn = new OleDbConnection(connetionString); 
     try 
     { 
      cnn.Open(); 
      MessageBox.Show ("Connection Open ! "); 
      cnn.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Can not open connection ! "); 
     } 

curos