既然你的connection类只是返回一个conn,为什么不在开始的Connection conn=new Connection()后添加SqlConnection conn1=conn.conn();以后直接对conn1操作,至少容易懂哈。
给你篇数据库类的参考:(zz)
using System;
using System.Data;
using System.Data.OleDb;namespace DbClass
{
/// <summary>
/// Db_Class 的摘要说明。
/// </summary>
public class Db_Class
{
public OleDbConnection Conn;
//构造函数
public Db_Class()
{
Conn= new OleDbConnection("Provider=SQLOLEDB;Server=(local);Pwd=123456;UID=sa;Database=test");
}
//打开数据源链接
public OleDbConnection Db_Conn()
{
Conn.Open();
return Conn;
}
//返回DataReader数据集,下面的SQL可以动态生成
public OleDbDataReader Db_CreateReader(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
OleDbDataReader Rs = cmd.ExecuteReader();
return Rs;
this.close();
}
//返回DataReader数据集,下面的SQL是存储过程
public OleDbDataReader Db_CommandReader(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
cmd.CommandType = CommandType.StoredProcedure;
OleDbDataReader Rs = cmd.ExecuteReader();
return Rs;
this.close();
}
//返回数据DataSet数据集
public OleDbDataSet Db_CreateDataSet(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
OleDbDataAdapter Adpt= new OleDbDataAdapter(cmd,Conn);
DataSet Ds = new DataSet();
Adpt.Fill(Ds,"NewTable");
return Ds;
this.close();
}
//返回数据DataReader数据集,不需要返回数据的修改,删除可以使用本函数
public bool Db_ExecuteNonquery(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
try
{
OleDbDataReader Rs= cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
this.close();
}
//关闭数据链接
public void close()
{
Conn.Close();
} }
}
使用方法如下:using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace DbClass
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//string SQL="select * from sysfiles";
Db_Class Db_class = new Db_Class();
DataGrid1.DataSource=Db_class.Db_CommandReader("sp_tables");//使用SQLSERVER的存储过程。
DataGrid1.DataBind();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}
给你篇数据库类的参考:(zz)
using System;
using System.Data;
using System.Data.OleDb;namespace DbClass
{
/// <summary>
/// Db_Class 的摘要说明。
/// </summary>
public class Db_Class
{
public OleDbConnection Conn;
//构造函数
public Db_Class()
{
Conn= new OleDbConnection("Provider=SQLOLEDB;Server=(local);Pwd=123456;UID=sa;Database=test");
}
//打开数据源链接
public OleDbConnection Db_Conn()
{
Conn.Open();
return Conn;
}
//返回DataReader数据集,下面的SQL可以动态生成
public OleDbDataReader Db_CreateReader(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
OleDbDataReader Rs = cmd.ExecuteReader();
return Rs;
this.close();
}
//返回DataReader数据集,下面的SQL是存储过程
public OleDbDataReader Db_CommandReader(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
cmd.CommandType = CommandType.StoredProcedure;
OleDbDataReader Rs = cmd.ExecuteReader();
return Rs;
this.close();
}
//返回数据DataSet数据集
public OleDbDataSet Db_CreateDataSet(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
OleDbDataAdapter Adpt= new OleDbDataAdapter(cmd,Conn);
DataSet Ds = new DataSet();
Adpt.Fill(Ds,"NewTable");
return Ds;
this.close();
}
//返回数据DataReader数据集,不需要返回数据的修改,删除可以使用本函数
public bool Db_ExecuteNonquery(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
try
{
OleDbDataReader Rs= cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
this.close();
}
//关闭数据链接
public void close()
{
Conn.Close();
} }
}
使用方法如下:using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace DbClass
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//string SQL="select * from sysfiles";
Db_Class Db_class = new Db_Class();
DataGrid1.DataSource=Db_class.Db_CommandReader("sp_tables");//使用SQLSERVER的存储过程。
DataGrid1.DataBind();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}
解决方案 »
- -------------<%#Eval("ProductID") %>这样格式的跳转传值
- div中嵌套多个div怎么设置子div离父div的高度
- 首页调用分类栏目
- 100分请大家帮我看下为什么我这个for循环只能一次,是语法错误吗?
- 【viewstate】里究竟存放什么信息,在什么情况下可以禁用?
- 用C#写ASP.NET程序,DLL文件如何生成及在程序中调用啊?一直不解!
- 求一句SQL语言,返回符合条件的记录的总数目
- 在页面中调用公共类Users,出现问题,上下文中不存在名称“Users”
- 关于显示数据库中某条记录详细内容的问题
- 郁闷,散分
- 找*.asp文件中所有<%%>内容的正则表达式模式要怎么写?
- 请问还有什么工具是asp.net的开发平台
类 是一个单独 的DLL 的。
mydr=mycommand.ExecuteReader(CommandBehavior.CloseConnection);
后 F5 刷的手都酸了,也没有错误了,谢谢啊。顺便问一下,CommandBehavior.CloseConnection 也是在关闭 datareader 后自动关闭连接,那么我
mydr.Close();
conn.Conn().Close();
conn.Conn().Dispose();这几句也关闭连接了啊??为什么没有用??他们用法有什么区别?
所以你的conn.conn().Close()语句功能就是新建连接,打开连接然后关闭连接。SqlCommand mycommand=new SqlCommand("select * from admin_user",conn.Conn());
这个语句初始化了SqlComand对象的CommandText和Connection属性。所以你执行完mydr=mycommand.ExecuteReader();
while (mydr.Read())
{
Response.Write(mydr["id"]+"<br>");
}
这段代码后应该这样关闭对象
mydr.Close();
myCommand.Connection.Close();
======================>
mydr=mycommand.ExecuteReader(CommandBehavior.CloseConnection);当正在使用 SqlDataReader 时,关联的 SqlConnection 在忙于为 SqlDataReader 提供服务。当处于此状态时,除了关闭 SqlConnection 外,不能对其执行其他任何操作。在调用 SqlDataReader.Close 方法之前一直如此。如果创建了 SqlDataReader 并将 CommandBehavior 设置为 CloseConnection,则关闭 SqlDataReader 会自动关闭此连接。