新手上路,asp.net如何连接access2007的问题
新手开始学习asp.net,买了本书,结果里面的例子似乎不太成功,真是郁闷。asp.net连接access2007没能成功,请大家教教俺。用的visual studio 2005+Access2007=====================================
1.在web.config文件中添加了如下代码:
<appSettings>
<add key="ConnectString" value="Provider=Microsoft.ACE.OLEDB.12.0;DataSource=E:\Study\WebSite\AddressList\database1.accdb;PersistSecurityInfo=False"/>
</appSettings>
====================================
2.然后写了一个connection.aspx,里面只有一个按钮,点了之后去连接数据库:
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label>
</form>
========================================
3.connection.aspx.cs对应的按钮事件函数为:
protected void Button1_Click(object sender, EventArgs e)
{
int i = userNameValidate();
}
private int userNameValidate()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectString"]);
SqlCommand selectCmd = new SqlCommand("select * from Users where userId='doudou'", conn); //创建操作数据库对象
int i = 0;
try
{
conn.Open(); //打开连接
SqlDataReader sdr = selectCmd.ExecuteReader();//从数据库读取记录
if (sdr.Read())
{
i = 1;
lblMessage.Text = "此用户名已存在,请输入其他用户名!";//给出提示信息
}
else
{
lblMessage.Text = "此用户名可用!"; //给出提示信息
}
}
catch (System.Exception ee)
{
Response.Write("<script language=javascript>alert('" + ee.Message.ToString() + "')</script>");
}
finally
{
conn.Close();
}
return i;
}
========================
运行的时候报错:
Keyword not supported: 'provider'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Keyword not supported: 'provider'.
等等
编译器里面还有一些warning,问题好像是在web.config文件:
Could not find schema information for the element 'http://schemas.microsoft.com/.NetConfiguration/v2.0:configuration'.
Could not find schema information for the element 'http://schemas.microsoft.com/.NetConfiguration/v2.0:appSettings'.
Could not find schema information for the element 'http://schemas.microsoft.com/.NetConfiguration/v2.0:add'.
等等
请教这个web.config到底应该怎么写?
--------------------编程问答-------------------- 不能用SqlConnection,要用OleDbConnection --------------------编程问答-------------------- StringBuilder conStr = new StringBuilder();
conStr.Append("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=");
conStr.Append(path + @"Data\Graduation.mdb");
conStr.Append(";Jet OLEDB:Database");
conStr.Append(" Password=");
conStr.Append(";");
return conStr.ToString();
--------------------编程问答-------------------- 引用:
using System.Data.OleDb;
===============================
代码:
private int userNameValidate()
{
OleDbConnection conn = new OleDbConnection (ConfigurationManager.AppSettings["ConnectString"]);
OleDbCommand selectCmd = new OleDbCommand ("select * from Users where userId='doudou'", conn); //创建操作数据库对象
int i = 0;
try
{
conn.Open(); //打开连接
OleDbDataReader sdr = selectCmd.ExecuteReader();//从数据库读取记录
if (sdr.Read())
{
i = 1;
lblMessage.Text = "此用户名已存在,请输入其他用户名!";//给出提示信息
}
else
{
lblMessage.Text = "此用户名可用!"; //给出提示信息
}
}
==============================
你的数据源为access,而不是SQL Server;
--------------------编程问答-------------------- sql不可以这样的 楼上几位很清楚了…… --------------------编程问答-------------------- 恩。楼上的兄弟们说的对。
不过我感觉你那web.config文件中的DataSource=E:\Study\WebSite\AddressList\database1.accdb
最好用相对路径。 --------------------编程问答-------------------- 谢谢,我按照楼上说的做了,但是又出现了新的问题,运行程序的时候出现一个对话框:
Could not find installable ISAM
我网上查了一下,微软的KB里面说这个是Access2000的问题,但是我这个是2007,怎么也会有这个问题呢?
晕啊,真不顺利! --------------------编程问答-------------------- LZ的代码如果是access 2003 是可以运行的,但access2007 ,我测试了一下也打不开,跟你一起等答案 --------------------编程问答--------------------
<add name="db1ConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\data.accdb" providerName="System.Data.OleDb" />
补充:.NET技术 , ASP.NET