为什么相同的代码在windows应用程序跟asp.net运行结果不一样呢

xinbuzailiulei 2004-12-09 05:51:45
下面这段代码在windows应用程序下可以调试通过,而在asp.net环境下缺报错,说“操作必须使用一个可更新的查询”,并且错误发生在最后一句,之前的都正确。
哪位能帮忙看一下啊。谢
OleDbConnection oConnection=new OleDbConnection();
string oConnectionString;
DataSet oDataSet=new DataSet();
OleDbDataAdapter oOleDbAdapter=new OleDbDataAdapter();
string DataBaseFile=@"E:\test\test.mdb";
oConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source="+DataBaseFile;
oConnection.ConnectionString=oConnectionString;
oConnection.Open();

OleDbCommand oSelectCommand=new OleDbCommand();
oSelectCommand.CommandText="SELECT * FROM information";
oSelectCommand.Connection=oConnection;

OleDbCommand oInsertCommand=new OleDbCommand();
oInsertCommand.CommandText="INSERT INTO information
VALUES(?,?,?)";
oInsertCommand.Connection=oConnection;
OleDbParameterCollection
oInsertParameters=oInsertCommand.Parameters;
OleDbParameter oInsertParameter1=oInsertCommand.CreateParameter();
oInsertParameter1.SourceColumn="name";
OleDbParameter oInsertParameter2=oInsertCommand.CreateParameter();
oInsertParameter2.SourceColumn="age";
OleDbParameter oInsertParameter3=oInsertCommand.CreateParameter();
oInsertParameter3.SourceColumn="sex";

oInsertParameters.Add(oInsertParameter1);
oInsertParameters.Add(oInsertParameter2);
oInsertParameters.Add(oInsertParameter3);

oOleDbAdapter.SelectCommand=oSelectCommand;
oOleDbAdapter.InsertCommand=oInsertCommand;


oOleDbAdapter.Fill(oDataSet);
MessageBox.Show(oDataSet.Tables[0].Rows.Count.ToString());



DataRow oDataRow=oDataSet.Tables[0].NewRow();
oDataRow["name"]="dd";
oDataRow["age"]="dds";
oDataRow["sex"]="sdfs";
oDataSet.Tables[0].Rows.Add(oDataRow);
MessageBox.Show(oDataSet.Tables[0].Rows.Count.ToString());

oOleDbAdapter.Update(oDataSet);
...全文
143 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
The123 2004-12-10
  • 打赏
  • 举报
回复
但我右击鼠标的时候,发现‘属性’下没有‘安全’这个选项。只有常规,共享,web共享,自定义四个选项。

http://support.microsoft.com/default.aspx?scid=kb;zh-cn;308421&sd=tech
The123 2004-12-10
  • 打赏
  • 举报
回复
像任何应用程序一样,Web 应用程序需要访问不同的系统资源(如文件或数据库)。这些资源通常由授予不同用户和组的访问级别保护。

在 Web 应用程序中,它可能不是请求直接访问的应用程序用户。相反,用户使用浏览器与 Web 应用程序进行通信,然后由 Web 应用程序请求资源。此间接访问会带来访问权限问题。

有几种策略可用于管理 Web 应用程序中的访问权限。以下几节将提供每个选项的概述。主题中的链接指向包含更多信息的主题。

通过 Windows 集成安全性进行访问
如果您的 Web 应用程序运行在 Intranet 环境中(即所有用户都运行 Windows 并在同一防火墙之后),则应用程序可以使用 Windows 集成安全性访问资源。在此策略中,所有用户一登录到 Windows 就对他们进行身份验证。身份验证进程将生成一个包含该用户信息的标记。(标记不包含用户的凭据,仅包含指示已对该用户进行身份验证的代码。)在请求本地资源时,Web 应用程序可以使用此标记。例如,已登录的用户在 Internet Explorer 中请求 Web 窗体页。请求将被传递到 Internet 信息服务 (IIS),然后传递到 ASP.NET,ASP.NET 可以使用用户的身份验证标记请求访问服务器计算机上的文件。

Windows 集成安全性通常是 Web 访问权限的最安全选项。如果在您的应用程序中是可行的,则应该将应用程序配置为使用此选项。

有关详细信息,请参见 ASP.NET 身份验证。

使用 ASP.NET 用户帐户的匿名访问
在大多数应用程序中,用户通常匿名访问 Web 应用程序(即最初他们不提供身份验证凭据)。在这种情况下,Web 应用程序必须采用(模拟)它可以用于请求资源的某个标识。

默认情况下,当 ASP.NET 允许使用匿名访问时,应用程序运行在特殊的本地(不是域)帐户的上下文中。用户上下文因操作系统而异。在 Windows 2000 和 Windows XP 中,应用程序运行在名为 ASPNET 的帐户之下。在 Windows Server 2003 中,用户上下文称为 NETWORK SERVICE。这些用户帐户是在 .NET Framework 安装过程中创建的,它具有唯一的不易破解的密码,并仅被授予有限的权限。ASPNET 或 NETWORK SERVICE 用户只能访问运行 Web 应用程序所需的特定文件夹,如 Web 应用程序存储已编译文件的 \bin 目录。

注意 IIS 具有将匿名用户映射到特定本地用户或域用户的类似机制。您可以使用 IIS 工具重新映射应用程序中所有文件的匿名访问,而不仅仅映射 ASP.NET 文件(Web 窗体页和控件)。有关详细信息,请参见 Microsoft Internet Information Server 安全性概述。
如果您的 Web 应用程序需要访问 Web 服务器计算机上的其他资源,则可以根据需要向 ASPNET 或 NETWORK SERVICE 用户授予权限。例如,如果应用程序需要将一个文件写入目录,则需要将写入权限显式授予该目录中的 ASPNET 或 NETWORK SERVICE 帐户。

或者,您可以在请求资源时为要使用的 ASP.NET 指定另一个用户标识。您可以指定以其名义发出请求的用户名(本地用户或域用户)。有关详细信息,请参见 ASP.NET 进程标识。有关详细信息,请参见 <processModel> 元素。或者您可以指定 ASP.NET 改为在系统(管理员)上下文中运行。

警告 在系统上下文中运行 ASP.NET 应用程序,就允许这些应用程序使用服务器上的管理权限运行。如果用户可以控制您的应用程序,那么他们就可以随意访问服务器上的许多重要资源。
默认情况下,您使用默认的用户上下文无法访问某些资源,因为它们要求访问需要管理级别特权的资源。例如,如果您的应用程序需要使用 System.Diagnostics 命名空间中的方法创建新的事件日志类别,如果它运行在 ASPNET 或 NETWORK SERVICE 用户的上下文中,则无法创建新的事件日志类别。在这种情况下,您需要更改应用程序上下文,或者进行更改使 ASP.NET 用户帐户可以访问事件日志。

更多内容请访问:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/vbcon/html/vbconAccessPermissionsForWebApplications.htm

xinbuzailiulei 2004-12-10
  • 打赏
  • 举报
回复
可能我说的不太明白。我说的asp.net环境是说在vs.net中新建的asp.net应用程序中。并且用户都是管理员性质的。
分区ntfs,系统winxp.
但我右击鼠标的时候,发现‘属性’下没有‘安全’这个选项。只有常规,共享,web共享,自定义四个选项。
这是所有的网页的代码。另,我的test.mdb就是name,age,sex三列。
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="true" Inherits="test.WebForm1" %>
<%@Import Namespace="System.Data.OleDb"%>
<%@Import Namespace="System.Data"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
<script language="c#" runat="server">
void Page_Load(object sender,EventArgs e){
OleDbConnection oConnection=new OleDbConnection();
string oConnectionString;
DataSet oDataSet=new DataSet();
OleDbDataAdapter oOleDbAdapter=new OleDbDataAdapter();
string DataBaseFile=@"E:\test\test.mdb";
oConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DataBaseFile;
oConnection.ConnectionString=oConnectionString;
oConnection.Open();

OleDbCommand oSelectCommand=new OleDbCommand();
oSelectCommand.CommandText="SELECT * FROM information";
oSelectCommand.Connection=oConnection;

OleDbCommand oInsertCommand=new OleDbCommand();
oInsertCommand.CommandText="INSERT INTO information VALUES(?,?,?)";
oInsertCommand.Connection=oConnection;
OleDbParameterCollection oInsertParameters=oInsertCommand.Parameters;
OleDbParameter oInsertParameter1=oInsertCommand.CreateParameter();
oInsertParameter1.SourceColumn="name";
OleDbParameter oInsertParameter2=oInsertCommand.CreateParameter();
oInsertParameter2.SourceColumn="age";
OleDbParameter oInsertParameter3=oInsertCommand.CreateParameter();
oInsertParameter3.SourceColumn="sex";

oInsertParameters.Add(oInsertParameter1);
oInsertParameters.Add(oInsertParameter2);
oInsertParameters.Add(oInsertParameter3);





oOleDbAdapter.SelectCommand=oSelectCommand;
oOleDbAdapter.InsertCommand=oInsertCommand;


oOleDbAdapter.Fill(oDataSet);
//MessageBox.Show(oDataSet.Tables[0].Rows.Count.ToString());



DataRow oDataRow=oDataSet.Tables[0].NewRow();
oDataRow["name"]="222222221dd";
oDataRow["age"]="22222211111dds";
oDataRow["sex"]="22222222211111sdfs";
oDataSet.Tables[0].Rows.Add(oDataRow);
//MessageBox.Show(oDataSet.Tables[0].Rows.Count.ToString());

oOleDbAdapter.Update(oDataSet);


}


</script>
</head>
<body MS_POSITIONING="GridLayout">

<form id="Form1" method="post" runat="server">

</form>

</body>
</html>
The123 2004-12-10
  • 打赏
  • 举报
回复
1你的程序“在asp.net环境下”的用户是ASPNET,
2你的程序“在windows应用程序下”的用户是你开机进入操作系统时的用户(8成是administrator吧),

你的数据库在这里E:\test\test.mdb

只要你让ASPNET这个用户有权限对E:\test\这个文件夹有写的权限就可以。

NTFS格式的分区吗?鼠标右键->属性->安全,look look :return csdn.

chinull 2004-12-10
  • 打赏
  • 举报
回复
是不是因为string DataBaseFile=@"E:\test\test.mdb";是个本地磁盘路径,没有写权限哦
改成虚拟路径试试看。
xinbuzailiulei 2004-12-10
  • 打赏
  • 举报
回复

下面是我"windows应用程序"里面的全部代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
OleDbConnection oConnection=new OleDbConnection();
string oConnectionString;
DataSet oDataSet=new DataSet();
OleDbDataAdapter oOleDbAdapter=new OleDbDataAdapter();
string DataBaseFile=@"E:\test\test.mdb";
oConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DataBaseFile;
oConnection.ConnectionString=oConnectionString;
oConnection.Open();

OleDbCommand oSelectCommand=new OleDbCommand();
oSelectCommand.CommandText="SELECT * FROM information";
oSelectCommand.Connection=oConnection;

OleDbCommand oInsertCommand=new OleDbCommand();
oInsertCommand.CommandText="INSERT INTO information VALUES(?,?,?)";
oInsertCommand.Connection=oConnection;
OleDbParameterCollection oInsertParameters=oInsertCommand.Parameters;
OleDbParameter oInsertParameter1=oInsertCommand.CreateParameter();
oInsertParameter1.SourceColumn="name";
OleDbParameter oInsertParameter2=oInsertCommand.CreateParameter();
oInsertParameter2.SourceColumn="age";
OleDbParameter oInsertParameter3=oInsertCommand.CreateParameter();
oInsertParameter3.SourceColumn="sex";

oInsertParameters.Add(oInsertParameter1);
oInsertParameters.Add(oInsertParameter2);
oInsertParameters.Add(oInsertParameter3);





oOleDbAdapter.SelectCommand=oSelectCommand;
oOleDbAdapter.InsertCommand=oInsertCommand;


oOleDbAdapter.Fill(oDataSet);
MessageBox.Show(oDataSet.Tables[0].Rows.Count.ToString());



DataRow oDataRow=oDataSet.Tables[0].NewRow();
oDataRow["name"]="111111111111dd";
oDataRow["age"]="11111111111111dds";
oDataRow["sex"]="111111111111sdfs";
oDataSet.Tables[0].Rows.Add(oDataRow);
MessageBox.Show(oDataSet.Tables[0].Rows.Count.ToString());

oOleDbAdapter.Update(oDataSet);
}
}
}
下面是我"asp.net应用程序"里面的全部代码<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="true" Inherits="test.WebForm1" %>
<%@Import Namespace="System.Data.OleDb"%>
<%@Import Namespace="System.Data"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
<script language="c#" runat="server">
void Page_Load(object sender,EventArgs e){
OleDbConnection oConnection=new OleDbConnection();
string oConnectionString;
DataSet oDataSet=new DataSet();
OleDbDataAdapter oOleDbAdapter=new OleDbDataAdapter();
string DataBaseFile=@"E:\test\test.mdb";
oConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DataBaseFile;
oConnection.ConnectionString=oConnectionString;
oConnection.Open();

OleDbCommand oSelectCommand=new OleDbCommand();
oSelectCommand.CommandText="SELECT * FROM information";
oSelectCommand.Connection=oConnection;

OleDbCommand oInsertCommand=new OleDbCommand();
oInsertCommand.CommandText="INSERT INTO information VALUES(?,?,?)";
oInsertCommand.Connection=oConnection;
OleDbParameterCollection oInsertParameters=oInsertCommand.Parameters;
OleDbParameter oInsertParameter1=oInsertCommand.CreateParameter();
oInsertParameter1.SourceColumn="name";
OleDbParameter oInsertParameter2=oInsertCommand.CreateParameter();
oInsertParameter2.SourceColumn="age";
OleDbParameter oInsertParameter3=oInsertCommand.CreateParameter();
oInsertParameter3.SourceColumn="sex";

oInsertParameters.Add(oInsertParameter1);
oInsertParameters.Add(oInsertParameter2);
oInsertParameters.Add(oInsertParameter3);





oOleDbAdapter.SelectCommand=oSelectCommand;
oOleDbAdapter.InsertCommand=oInsertCommand;


oOleDbAdapter.Fill(oDataSet);
//MessageBox.Show(oDataSet.Tables[0].Rows.Count.ToString());



DataRow oDataRow=oDataSet.Tables[0].NewRow();
oDataRow["name"]="222222221dd";
oDataRow["age"]="22222211111dds";
oDataRow["sex"]="22222222211111sdfs";
oDataSet.Tables[0].Rows.Add(oDataRow);
//MessageBox.Show(oDataSet.Tables[0].Rows.Count.ToString());

oOleDbAdapter.Update(oDataSet);


}


</script>
</head>
<body MS_POSITIONING="GridLayout">

<form id="Form1" method="post" runat="server">

</form>

</body>
</html>
xinbuzailiulei 2004-12-10
  • 打赏
  • 举报
回复
对,就是最后一句有错,如果所有的这些在windows应用程序环境下是可以正确运行,能够顺利往我的access里面写入新值的。
但如果放在asp.net环境中,如果注释掉最后一句oOleDbAdapter.Update(oDataSet);则也可以顺利执行。如果不注释的话,提示出错“操作必须使用一个可更新的查询”.
The123 2004-12-09
  • 打赏
  • 举报
回复
oOleDbAdapter.Update(oDataSet);
是这句出错吗?
xinbuzailiulei 2004-12-09
  • 打赏
  • 举报
回复
The123大哥在吗?
xinbuzailiulei 2004-12-09
  • 打赏
  • 举报
回复
如果把最后一句注释掉,然后将MessageBox.Show(oDataSet.Tables[0].Rows.Count.ToString());改为Response.Write(oDataSet.Tables[0].Rows.Count.ToString());,两个Response.Write(oDataSet.Tables[0].Rows.Count.ToString()),分别返回1,2.
下面是我的权限设置
访问权限 读取(选中),写入(选中),脚本资源访问(选中),
应用程序权限:执行(包括脚本)
还不行。
zhouyong2052 2004-12-09
  • 打赏
  • 举报
回复
up
The123 2004-12-09
  • 打赏
  • 举报
回复
权限。

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧