FineUI 官方论坛
标题:
expasp连接oracle问题求助
[打印本页]
作者:
zzq
时间:
2013-7-9 13:53
标题:
expasp连接oracle问题求助
本人学习根据联动条件和分页进行查询设计过程中,用extaspnet在线示例进行修改出现如图错误,求助!web.config 配置
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=sc
ersist Security Info=True;User ID=readonly
assword=123;Unicode=True" providerName="System.Data.OracleClient" />
</connectionStrings>
dj_nsrdnbm.aspx 文件如下:
<form id="form1" runat="server">
<ext:PageManager ID="PageManager1" runat="server" />
<ext:Panel ID="Panel7" runat="server" EnableBackgroundColor="true" BodyPadding="3px"
ShowBorder="True" ShowHeader="True" Width="800px" Height="350px" Title="查询条件"
Layout="Anchor">
<Items>
<ext:Form ID="Form5" ShowBorder="False" BodyPadding="5px" AnchorValue="100%" EnableBackgroundColor="true"
ShowHeader="False" runat="server">
<Rows>
<ext:FormRow>
<Items>
<ext:DropDownList ID="zgswjg_ddl" Label="主管税务机关" runat="server" AutoPostBack="True" OnSelectedIndexChanged="zgswjg_ddl_SelectedIndexChanged">
</ext:DropDownList>
<ext:DropDownList ID="zgy_ddl" Label="管理员" runat="server">
</ext:DropDownList>
</Items>
</ext:FormRow>
<ext:FormRow>
<Items>
<ext:Button ID="Button1" Text="查询" runat="server" OnClick="Button1_Click">
</ext:Button>
</Items>
</ext:FormRow>
</Rows>
</ext:Form>
<ext:Panel ID="Panel8" ShowBorder="True" ShowHeader="false" AnchorValue="100% -62"
Layout="Fit" runat="server">
<Items>
<ext:Grid ID="Grid1" Title="查询结果" Width="800px" PageSize="20" ShowBorder="true" ShowHeader="true"
AutoHeight="true" AllowPaging="true" runat="server" EnableCheckBoxSelect="True"
DataKeyNames="Id,Name" IsDatabasePaging="true" OnPageIndexChange="Grid1_PageIndexChange"
EnableRowNumber="True">
<Columns>
<ext:BoundField Width="100px" DataField="taxper_id" DataFormatString="{0}" HeaderText="纳税人电脑编码" />
<ext:BoundField Width="300px" DataField="taxper_name" HeaderText="纳税人名称" />
<ext:BoundField Width="300px" DataField="duty_office_name" HeaderText="税务机关" />
<ext:BoundField Width="100px" DataField="staff_name" HeaderText="管理员" />
</Columns>
</ext:Grid>
</Items>
</ext:Panel>
</Items>
</ext:Panel>
</form>
dj_nsrdnbm.aspx.cs文件如下:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using xjy.DAL;
namespace ExtAspNet.Examples.grid
{
public partial class grid_database_paging : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
BindZgswjg();
BindZgy();
}
}
protected void BindZgswjg()
{
OraDbHelper action = new OraDbHelper();
string selectzgswjg = "select duty_office_code,duty_office_name from pc_area_code";
zgswjg_ddl.DataSource = action.ExecuteDataTable(selectzgswjg);
zgswjg_ddl.DataTextField = "duty_office_name";
zgswjg_ddl.DataValueField = "duty_office_code";
zgswjg_ddl.DataBind();
zgswjg_ddl.Items.Insert(0, "[主管税务机关]");
zgswjg_ddl.Items[0].Value = "";
}
protected void BindZgy()
{
OraDbHelper action = new OraDbHelper();
string selectzgy = "select staff_code,staff_name from pc_staff where duty_office_code='" + zgswjg_ddl.SelectedValue + "'";
zgy_ddl.DataSource = action.ExecuteDataTable(selectzgy);
zgy_ddl.Items.Clear();
zgy_ddl.DataTextField = "staff_name";
zgy_ddl.DataValueField = "staff_code";
zgy_ddl.DataBind();
zgy_ddl.Items.Insert(0, "[请选择--管理员]");
zgy_ddl.Items[0].Value = "";
}
protected void zgswjg_ddl_SelectedIndexChanged(object sender, EventArgs e)
{
zgy_ddl.Items.Clear();
BindZgy();
}
#region BindGrid
private void BindGrid()
{
// 1.设置总项数
Grid1.RecordCount = GetTotalCount();
// 2.获取当前分页数据
DataTable table = GetPagedDataTable(Grid1.PageIndex, Grid1.PageSize);
// 3.绑定到Grid
Grid1.DataSource = table;
Grid1.DataBind();
}
/// <summary>
/// 模拟返回总项数
/// </summary>
/// <returns></returns>
private int GetTotalCount()
{
return GetDataTable().Rows.Count;
}
/// <summary>
/// 模拟数据库分页
/// </summary>
/// <returns></returns>
private DataTable GetPagedDataTable(int pageIndex, int pageSize)
{
DataTable source = GetDataTable();
DataTable paged = source.Clone();
int rowbegin = pageIndex * pageSize;
int rowend = (pageIndex + 1) * pageSize;
if (rowend > source.Rows.Count)
{
rowend = source.Rows.Count;
}
for (int i = rowbegin; i < rowend; i++)
{
paged.ImportRow(source.Rows[i]);
}
return paged;
}
#endregion
#region Events
protected void Button1_Click(object sender, EventArgs e)
{
labResult.Text = HowManyRowsAreSelected(Grid1);
}
protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
{
Grid1.PageIndex = e.NewPageIndex;
BindGrid();
}
#endregion
}
}
复制代码
作者:
zzq
时间:
2013-7-9 13:57
补充
OraDbHelper.cs文件如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OracleClient;
namespace xjy.DAL
{
/// OraDbHelper 对ORACLE 数据库操作的通用类
public class OraDbHelper
{
private string connectionString;
/// 设置数据库字符串的连接
public string ConnectionString
{
set { connectionString = value; }
}
public OraDbHelper()
{
this.connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; ;
}
#region ExecuteDataTable
/// 执行一个查询,并返回结果集
/// <param name="commandText">要执行的查询SQL文本命令</param>
/// <returns>返回查询结果集</returns>
public DataTable ExecuteDataTable(string commandText)
{
return ExecuteDataTable(commandText, CommandType.Text, null);
}
/// 执行一个查询,并返回查询结果
/// <param name="commandText">要执行的SQL语句</param>
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param>
/// <returns>返回查询结果集</returns>
public DataTable ExecuteDataTable(string commandText, CommandType commandType)
{
return ExecuteDataTable(commandText, commandType, null);
}
/// 执行一个查询,并返回查询结果
/// <param name="commandText">要执行的SQL语句</param>
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param>
/// <param name="parameters">PL-SQL 语句或存储过程的参数数组</param>
/// <returns></returns>
public DataTable ExecuteDataTable(string commandText, CommandType commandType, params OracleParameter[] parameters)
{
DataTable data = new DataTable();//实例化DataTable,用于装载查询结果集;
OracleConnection connection = new OracleConnection(connectionString);
OracleCommand command = new OracleCommand(commandText, connection);
command.CommandType = commandType;//设置command的CommandType为指定的CommandType
//如果同时传入了参数,则添加这些参数
if (parameters != null)
{
foreach (OracleParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
//通过包含查询SQL的SqlCommand实例来实例化SqlDataAdapter
OracleDataAdapter adapter = new OracleDataAdapter(command);
adapter.Fill(data);//填充DataTable
return data;
}
#endregion ExecuteDataTable
#region ExecuteReader
public OracleDataReader ExecuteReader(string commandText)
{
return ExecuteReader(commandText, CommandType.Text, null);
}
/// 将 CommandText 发送到 Connection 并生成一个 OracleDataReader。
/// <param name="commandText">要执行的SQL语句</param>
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param>
public OracleDataReader ExecuteReader(string commandText, CommandType commandType)
{
return ExecuteReader(commandText, commandType, null);
}
/// 将 CommandText 发送到 Connection 并生成一个 OracleDataReader。
/// <param name="commandText">要执行的SQL语句</param>
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param>
/// <param name="parameters">Transact-SQL 语句或存储过程的参数数组</param>
public OracleDataReader ExecuteReader(string commandText, CommandType commandType, OracleCommand[] parameters)
{
OracleConnection connection = new OracleConnection(connectionString);
OracleCommand command = new OracleCommand(commandText, connection);
command.CommandType = commandType;
//如果同时传入了参数,则添加这些参数
if (parameters != null)
{
foreach (OracleCommand parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
connection.Open();
//CommandBehavior.CloseConnection参数指示关闭Reader对象时关闭与其关联的Connection对象
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
#endregion ExecuteReader
#region ExecuteScalar
/// 从数据库中检索单个值(例如一个聚合值)。
/// <param name="commandText">要执行的查询PL-SQL文本命令</param>
public Object ExecuteScalar(string commandText)
{
return ExecuteScalar(commandText, CommandType.Text, null);
}
/// 从数据库中检索单个值(例如一个聚合值)。
/// <param name="commandText">要执行的SQL语句</param>
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者PL-SQL文本命令</param>
/// <returns></returns>
public Object ExecuteScalar(string commandText, CommandType commandType)
{
return ExecuteScalar(commandText, commandType, null);
}
public Object ExecuteScalar(string commandText, CommandType commandType, OracleParameter[] parameters)
{
object result = null;
OracleConnection connection = new OracleConnection(connectionString);
OracleCommand command = new OracleCommand(commandText, connection);
command.CommandType = commandType;//设置command的CommandType为指定的CommandType
//如果同时传入了参数,则添加这些参数
if (parameters != null)
{
foreach (OracleParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
connection.Open();//打开数据库连接
result = command.ExecuteScalar();
connection.Close();
return result;//返回查询结果的第一行第一列,忽略其它行和列
}
#endregion ExecuteScalar
#region ExecuteNonQuery
public int ExecuteNonQuery(string commandText)
{
return ExecuteNonQuery(commandText, CommandType.Text, null);
}
public int ExecuteNonQuery(string commandText, CommandType commandType)
{
return ExecuteNonQuery(commandText, commandType, null);
}
/// <summary>
/// 对数据库执行增删改操作
/// </summary>
/// <param name="commandText">要执行的pl-sql语句</param>
/// <param name="commandType">要执行的查询语句的类型,如存储过程或者pl-sql文本命令</param>
/// <param name="parameters">pl-sql 语句或存储过程的参数数组</param>
/// <returns>返回执行操作受影响的行数</returns>
public int ExecuteNonQuery(string commandText, CommandType commandType, OracleParameter[] parameters)
{
int count = 0;
OracleConnection connection = new OracleConnection(connectionString);
OracleCommand command = new OracleCommand(commandText, connection);
command.CommandType = commandType;//设置command的CommandType为指定的CommandType
//如果同时传入了参数,则添加这些参数
if (parameters != null)
{
foreach (OracleParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
connection.Open();//打开数据库连接
count = command.ExecuteNonQuery();
connection.Close();
return count;//返回执行增删改操作之后,数据库中受影响的行数
}
#endregion ExecuteNonQuery
}
}
欢迎光临 FineUI 官方论坛 (https://fineui.com/BBS/)
Powered by Discuz! X3.4