首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用OleDbConnection连接

使用OleDbConnection连接
EN

Stack Overflow用户
提问于 2012-03-23 07:04:43
回答 3查看 73.1K关注 0票数 4

我正在尝试连接到具有两个表的数据库。但是,在我尝试登录后,我遇到了一个错误。错误说明在零点处没有行。我想这是因为我的关系:

代码语言:javascript
复制
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))
        {
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-23 07:17:19

您需要打开连接

代码语言:javascript
复制
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);
票数 9
EN

Stack Overflow用户

发布于 2012-11-10 00:21:48

您不需要打开连接。如果OleDbDataAdapter.Fill发现连接一开始就关闭,它将打开连接并将其关闭。Fill将连接保留在找到它的状态。

我确实质疑你的SQL。我对OleDb的理解是,它不支持在SQL中命名参数。它需要一个占位符“?而不是@login。您需要为每个占位符提供一个参数,并且必须按参数出现的顺序添加参数。如果您的SQL无效,那么您将在DataTable中出现SQL异常或没有数据,即没有第0行!

票数 6
EN

Stack Overflow用户

发布于 2013-05-04 18:08:12

oledb连接完整代码

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

代码语言:javascript
复制
        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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9831843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档