日期:2014-05-18  浏览次数:20708 次

.net中如何与Access数据库连接
小弟 初学.net不知道 在做一个登陆界面的时候 用的是vs2005 在双击登陆按钮后应该在甚么地方 添加些甚么内容 才能把这个登陆网页和数据库连接在一起.在我们进行登陆的时候能够对登陆的用户进行验证输入是否正确.

------解决方案--------------------
楼主,2楼给你的代码是一个示例,
#region Using directives 

using System; 
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic; 
using System.Text; 

#endregion 

namespace ReadingAccessData 

class Program 

static void Main(string[] args) 

OleDbConnection thisConnection = new OleDbConnection( 
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\tmp\nwind.mdb"); 
//建立连接access数据库的对象

thisConnection.Open(); //打开链接

OleDbCommand thisCommand = thisConnection.CreateCommand(); 

thisCommand.CommandText =
"SELECT CustomerID, CompanyName FROM Customers"; //设定要执行的SQL查询命令
OleDbDataReader thisReader = thisCommand.ExecuteReader(); //从数据库里名为Customers的表中取出
//名为CustomerID和CompanyName的字段

while (thisReader.Read()) 

Console.WriteLine("\t{0}\t{1}", 
thisReader["CustomerID"], thisReader["CompanyName"]); 
} //打印结果
thisReader.Close(); 
thisConnection.Close(); //关闭DataReader和数据库连接


Console.Write("Program finished, press Enter/Return to continue:"); 
Console.ReadLine(); 
 } 



至于具体你的项目代码该怎么写,2楼没有直接给出。要获得用户输入,应该是从控件里得到。至于数据库里查找数据的结果对应什么样的操作,更应该是你自己写的啊。
------解决方案--------------------
C# code

string username = txtUserName.Text;
string password = txtPassword.Text;
string sql = string.Format("SELECT * FROM Customers where username='{0}' and passwrod='{1}'",username,password);
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\tmp\nwind.mdb");//建立连接access数据库的对象 
OleDbCommand cmd = new OleDbCommand(sql,conn);  
conn.Open(); //打开链接 
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
   Response.Redirector("Admin/Default.aspx");
}
else
{
   Response.Write("javascript:alert('错误。。。。。');");
}
dr.Close();
conn.Close();