与OleDbConnection连接

问题描述 投票:4回答:3

我正在尝试连接到具有两个表的数据库。但是,尝试登录后,出现错误。该错误表明零点处没有行。我认为这是由于我的联系:

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))
        {
c# asp.net visual-studio visual-studio-2010 visual-studio-2008
3个回答
9
投票

您需要打开连接

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);

6
投票

您不需要打开连接。 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

© www.soinside.com 2019 - 2024. All rights reserved.