.net中如何与Access数据库连接 小弟 初学.net不知道 在做一个登陆界面的时候 用的是vs2005 在双击登陆按钮后应该在甚么地方 添加些甚么内容 才能把这个登陆网页和数据库连接在一起.在我们进行登陆的时候能够对登陆的用户进行验证输入是否正确. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 点登陆按钮之后,就会产生一个单击事件,然后在事件里写连接access数据库的代码,.net连接access数据库一般用System.Data.Oledb我给你个例子:#region Using directivesusing System;using System.Data; using System.Data.OleDb; using System.Collections.Generic;using System.Text;#endregionnamespace ReadingAccessData{ class Program { static void Main(string[] args) { OleDbConnection thisConnection = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\tmp\nwind.mdb"); thisConnection.Open(); OleDbCommand thisCommand = thisConnection.CreateCommand(); thisCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers"; OleDbDataReader thisReader = thisCommand.ExecuteReader(); while (thisReader.Read()) { Console.WriteLine("\t{0}\t{1}", thisReader["CustomerID"], thisReader["CompanyName"]); } thisReader.Close(); thisConnection.Close(); Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); } }}一个.NET技术群,希望有开发经验的朋友加入,本群适合.net中高级开发人员。 1群已经满,请加2群,群号:28042466 热烈欢迎从事.net开发的程序员加入。不管你专于c#还是vb.net,都可以参加本群。 楼主,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楼没有直接给出。要获得用户输入,应该是从控件里得到。至于数据库里查找数据的结果对应什么样的操作,更应该是你自己写的啊。 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();没有现成代码,现在也没有工具,纯手工打得,如果有大小写或名字错误请自己改正 Login控件只能用SQLServer2005数据库,他用的是Access 呵呵,不是吧,我还用Login控件连过LDAP。 这个兄弟,建议你去找一些源代码看看 http://www.51aspx.com 上都有很开开源的代码,也有用Access的。 Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\test.mdb;Persist Security Info=False把上面的地址改成你的access所在的地址就行了 在线等,求正则,200分不成敬意... ... Winform中,SaveFileDialog保存文件的时候为什么不能新建 C#编写邮件群发软件的问题 求帮忙,软件测试 spread 的Excel输出问题 放100分提问,关键字 搜索,分词,and 查询 默认按钮,取消按钮 .NET如何做个用户自己的控件,然后放到工具箱里去? 除了FolderBrowserDialog,还有没有别的对话框可以选择文件夹? 验证码问题,纠结死我了 如何防止多用户登陆同一账号的问题(在线等,解决送分!) C# 控件释放问题
我给你个例子:
#region Using directivesusing System;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;#endregionnamespace ReadingAccessData
{
class Program
{
static void Main(string[] args)
{
OleDbConnection thisConnection = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\tmp\nwind.mdb"); thisConnection.Open(); OleDbCommand thisCommand = thisConnection.CreateCommand(); thisCommand.CommandText =
"SELECT CustomerID, CompanyName FROM Customers"; OleDbDataReader thisReader = thisCommand.ExecuteReader(); while (thisReader.Read())
{
Console.WriteLine("\t{0}\t{1}",
thisReader["CustomerID"], thisReader["CompanyName"]);
}
thisReader.Close();
thisConnection.Close();
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
} }
}
一个.NET技术群,希望有开发经验的朋友加入,本群适合.net中高级开发人员。
1群已经满,请加2群,群号:28042466
热烈欢迎从事.net开发的程序员加入。不管你专于c#还是vb.net,都可以参加本群。
#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楼没有直接给出。要获得用户输入,应该是从控件里得到。至于数据库里查找数据的结果对应什么样的操作,更应该是你自己写的啊。
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();没有现成代码,现在也没有工具,纯手工打得,如果有大小写或名字错误请自己改正
把上面的地址改成你的access所在的地址就行了