简体   繁体   English

与 OleDbConnection 连接

[英]Connect with OleDbConnection

I am trying to connect to a database with two tables.我正在尝试连接到具有两个表的数据库。 However, after I try and log in, I have an error.但是,在我尝试登录后,出现错误。 The error says there is no row at spot zero.错误说零点没有行。 I think this is because of my connection:我认为这是因为我的联系:

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 Login=@login", 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))
        {

You need to open the connection你需要打开连接

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 Login=@login", 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);

You do NOT need to open the connection.您不需要打开连接。 OleDbDataAdapter.Fill will open the connection AND close it if it found it closed to start with. OleDbDataAdapter.Fill 将打开连接并关闭它,如果它发现它开始关闭。 Fill leaves the connection in the state that it finds it. Fill 将连接保持在它找到它的状态。

I do question your SQL.我确实质疑您的 SQL。 My understanding of OleDb is that it does NOT support naming parameters in SQL.我对 OleDb 的理解是它不支持 SQL 中的命名参数。 It needs a place holder "?"它需要一个占位符“?” instead of @login.而不是@login。 You need a Parameter for each placeholder and the parameters must be added in the order they occurr.每个占位符都需要一个参数,并且必须按照参数出现的顺序添加参数。 If your SQL is not valid, then you will have either a SQL Exception or no data in the DataTable, ie NO row 0 !如果您的 SQL 无效,那么您将有 SQL 异常或 DataTable 中没有数据,即没有第 0 行!

oledb connection complete code oledb连接完整代码

http://csharp.net-informations.com/data-providers/csharp-oledb-connection.htm 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古罗斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM