【问题标题】:Connect with OleDbConnection与 OleDbConnection 连接
【发布时间】:2012-04-07 13:53:27
【问题描述】:

我正在尝试使用两个表连接到数据库。但是,在我尝试登录后,出现错误。错误说零点没有行。我想这是因为我的关系:

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

【问题讨论】:

  • 在初学者的连接上调用Open()怎么样?
  • 谢谢!我发现我的 OleDbCommand 也需要一个 try and catch 语句!
  • 我回滚到原来的标题。如果您对自己的问题有自己的答案,请将其作为答案发布并接受。请不要使用“已解决”编辑标题或使用答案编辑问题。
  • 我又回滚了。请阅读之前的评论。

标签: c# asp.net visual-studio visual-studio-2010 visual-studio-2008


【解决方案1】:

你需要打开连接

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

【讨论】:

  • 您的方法似乎缺少右括号。 ;)
【解决方案2】:

您不需要打开连接。 OleDbDataAdapter.Fill 将打开连接并在发现它开始时关闭它。 Fill 使连接处于它找到它的状态。

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

【讨论】:

    【解决方案3】:

    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 ! ");
            }
    

    游侠

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多