与OleDbConnection的连接 [英] Connect with OleDbConnection
本文介绍了与OleDbConnection的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用两个表连接到数据库。然而,当我尝试登录,我有一个错误。错误说,没有一行点零。我想这是因为我的连接任何帮助将是AP preciated的。
在先进的感谢!
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Data这;
使用System.Data.OleDb;命名空间项目3
{公共部分类_Default:System.Web.UI.Page
{ 保护无效的Page_Load(对象发件人,EventArgs的发送)
{ } 保护无效login_Click(对象发件人,EventArgs的发送)
{
OleDbConnection的连接=新的OleDbConnection(供应商= Microsoft.Jet.OLEDB.4.0;数据源= C:\\\\ \\\\用户\\\\ parodeghero文档\\\\的Visual Studio 2010 \\\\ \\\\项目\\\\项目3 \\\\项目3 \\\\的App_Data QA.mdb;坚持安全信息= TRUE);
//设置连接字符串
OleDbCommand的命令=新的OleDbCommand(从员工选择*其中登录= @登录,连接);
OleDbParameter参数0 =新OleDbParameter(@登陆,OleDbType.VarChar); param0.Value = employeeID.Text;
command.Parameters.Add(参数0); //中间层运行连接
OleDbDataAdapter的DA =新OleDbDataAdapter的(命令); 数据集DSET =新的DataSet(); da.Fill(DSET); //问题行
如果(dset.Tables [0] .Rows [0] [密码]的ToString()。等于(password.Text))
{
解决方案
您需要打开连接
保护无效login_Click(对象发件人,EventArgs的发送)
{
OleDbConnection的连接=新的OleDbConnection(供应商= Microsoft.Jet.OLEDB.4.0;数据源= C:\\\\ \\\\用户\\\\ parodeghero文档\\\\的Visual Studio 2010 \\\\ \\\\项目\\\\项目3 \\\\项目3 \\\\的App_Data QA.mdb;坚持安全信息= TRUE);
//设置连接字符串
OleDbCommand的命令=新的OleDbCommand(从员工选择*其中登录= @登录,连接);
OleDbParameter参数0 =新OleDbParameter(@登陆,OleDbType.VarChar); param0.Value = employeeID.Text;
command.Parameters.Add(参数0); 尝试
{
connect.Open();
}赶上(例外错误){的Debug.WriteLine(err.Message);} //中间层运行连接
OleDbDataAdapter的DA =新OleDbDataAdapter的(命令); 数据集DSET =新的DataSet(); da.Fill(DSET);
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 any help would be appreciated.
Thanks in advanced!
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);
这篇关于与OleDbConnection的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文