高手请进!!!!!!!!!!!!!!!!!!!!!!!!
大家好我是新手,在下刚学.net在网上下了一本wrox的高级编程,源码在wrox网站的下的,可是怎么每次碰到连接数据库绑定的例子都出错呀,Ass各Sql我都装了,就是找不到原因,请各位大侠帮帮忙吧:
错误如下:
Repeated-Value Data Binding to a DataReader Object
--------------------------------------------------------------------------------
* Error while accessing data.
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server 不存在或拒绝访问。
Microsoft OLE DB Provider for SQL Server
<ASP:DataGrid> control:
--------------------------------------------------------------------------------
[view source]
这是源码:
<%@Page Language="C#" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Repeated-Value Data Binding to a DataReader Object</title>
<style type="text/css">
body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input {font-family:Tahoma,Arial,sans-serif; font-size:9pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
</style></head>
<body bgcolor="#ffffff">
<span class="heading">Repeated-Value Data Binding to a DataReader Object</span><hr />
<!--------------------------------------------------------------------------->
<div id="outError" runat="server" />
<!-- Unlike the DataView we can only bind a object DataReader to one control -->
<!-- after it has bound the data the reader is at the end of the source rowset -->
<b><ASP:DataGrid></b> control:<br />
<ASP:DataGrid id="MyDataGrid" runat="server" /><p />
<!--------------------------------------------------------------------------->
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
// get connection string from web.config
string strConnect = ConfigurationSettings.AppSettings["DsnWroxBooksOleDb"];
// create a SQL statement to select some rows from the database
string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '%07645437%'";
// create a variable to hold an instance of a DataReader object
OleDbDataReader objDataReader;
try
{
// create a new Connection object using the connection string
OleDbConnection objConnect = new OleDbConnection(strConnect);
// open the connection to the database
objConnect.Open();
// create a new Command using the connection object and select statement
OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect);
// execute the SQL statement against the command to get the DataReader
objDataReader = objCommand.ExecuteReader();
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
+ objError.Message + "<br />" + objError.Source + "<p />";
return; // and stop execution
}
// set the DataSource property of the control
// a DataGrid can figure out the columns in the DataReader
// by itself, so we just set the DataSource property
MyDataGrid.DataSource = objDataReader;
MyDataGrid.DataBind(); // and bind the control
}
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
问题点数:0、回复次数:14Top
1 楼ld_thinking(懒得想)回复于 2005-06-04 10:27:08 得分 0
权限问题
把虚拟目录加everyone角色
或把整个目录考到别的地方重建虚拟目录Top
2 楼masong_1978(http://www.bd2shou.com)回复于 2005-06-04 10:28:35 得分 0
数据库都恢复了吗
最好用后端代码,不要写在前端!Top
3 楼LaoDai_Net(『老代』)回复于 2005-06-04 10:33:05 得分 0
TO ld_thinking(打击者)
它用的SQL Server 不存在权限问题吧,,,如果是请 给IUSR_XXX,IIS_PWG 加上权限
Top
4 楼LaoDai_Net(『老代』)回复于 2005-06-04 10:34:13 得分 0
请检查你的 web。config中的 appSettings 中 DsnWroxBooksOleDb 的值
贴出来看看应该是你的连接字符串出错Top
5 楼zhangaipl(烟酒僧)回复于 2005-06-04 10:35:46 得分 0
把你连接字符串贴出来看看。。Top
6 楼hackate(兰花开香入梦境,独思佳人亦飘然!!)回复于 2005-06-04 10:36:13 得分 0
string strConnect = ConfigurationSettings.AppSettings["DsnWroxBooksOleDb"];
好像应该是
string strConnect = ConfigurationSettings.AppSettings("DsnWroxBooksOleDb");吧
还有("DsnWroxBooksOleDb");里你估计写的是SQL连接方式。呵呵。我猜应该是这样。。Top
7 楼lywc(垃圾)回复于 2005-06-04 10:37:22 得分 0
我到底该听谁的Top
8 楼cjjll(潇潇风雨)回复于 2005-06-04 10:37:57 得分 0
对:请检查你的 web.config中的 appSettings 中 DsnWroxBooksOleDb 的值
贴出来看看应该是你的连接字符串出错
Top
9 楼georgezhou(happykidd)回复于 2005-06-04 10:38:14 得分 0
laodai_net正解
先看web.config DsnWroxBooksOleDb
怀疑server名称错
Top
10 楼cjjll(潇潇风雨)回复于 2005-06-04 10:38:47 得分 0
听我们的:不会有错Top
11 楼LaoDai_Net(『老代』)回复于 2005-06-04 10:40:58 得分 0
SQL Server 建议使用 SqlClient 不推荐使用OleDb,SqlClient 进行专门堆Sql Server进行过优化
OleDB 连接Sql Server 连接代码:
Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;
SqlClient 连接Sql Server 连接代码:
Server=Aron1;Database=pubs;User ID=sa;Password=asdasd;Top
12 楼lywc(垃圾)回复于 2005-06-04 10:51:28 得分 0
帮我看看:
<configuration>
<appSettings>
<add key="DsnNorthwind" value="server=.\NetSDK; database=Northwind; Trusted_Connection=yes" />
<add key="DsnPubs" value="server=.\NetSDK; database=pubs; Trusted_Connection=yes" />
<add key="DsnIBA" value="server=.\NetSDK; database=IBuyAdventure; Trusted_Connection=yes" />
<add key="DsnIBAOleDb" value="provider=SQLOLEDB.1;data source=.\NetSDK;initial catalog=IBuyAdventure; Trusted_Connection=yes" />
<add key="DsnWroxBooksSql" value="server=.\NetSDK; database=WroxBooks; Trusted_Connection=yes" />
<add key="DsnWroxBooksOleDb" value="provider=SQLOLEDB.1;data source=.\NetSDK;initial catalog=WroxBooks; Trusted_Connection=yes" />
<add key="DsnWroxBooksJet" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" />
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
Top
13 楼LaoDai_Net(『老代』)回复于 2005-06-04 11:09:23 得分 0
<add key="DsnWroxBooksOleDb" value="provider=SQLOLEDB.1;data source=.\NetSDK;initial catalog=WroxBooks; Trusted_Connection=yes" />
就是这句了
<add key="DsnWroxBooksOleDb" value="provider=SQLOLEDB.1;data source=改成(local)或者你的SQLSERVER服务器名;initial catalog=WroxBooks;User Id=用户名;Password=密码" />Top
14 楼sekiner(蓝色水分子)回复于 2005-06-04 12:03:57 得分 0
string strConnect = ConfigurationSettings.AppSettings["DsnWroxBooksOleDb"];
在web.config中,加入这个值没有
<appSettings>
<addkey="DsnWroxBooksOleDb"value="server=.;uid=sa;database=数据库"/>
</appSettings>Top




