using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.SqlClient;
using System.Data.Common;
namespace SqlConn
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//建立数据源链接
SqlConnection nwind_conn = new SqlConnection();
nwind_conn.ConnectionString = "server=localhost;database=Bookstore;uid=sa;pwd=sa; Connection Timeout=15; Packet Size=8192;Persist Security Info=true;";
//建立数据读取
SqlCommand nwind_comm = new SqlCommand();
nwind_comm.Connection = nwind_conn;
nwind_comm.CommandText = "SELECT book_name from book_info";
nwind_comm.CommandTimeout = 25;
nwind_comm.CommandType = CommandType.Text;
//打开数据源链接
nwind_conn.Open();
//建立事务,要建立事务须先打开和数据库的链接
SqlTransaction Tran;
Tran = nwind_conn.BeginTransaction();
nwind_comm.Transaction = Tran;
//结束并提交事务,应在数据读取前(即没有执行ExecuteNonQuery,ExecuteReader,ExecuteScalar,ExecuteXmlReader)前结束事务
Tran.Commit();
//或:Tran.Rollback();
//执行数据读取
SqlDataReader reader = nwind_comm.ExecuteReader();
//尝试取消命令的执行,如果尝试失败不会产生异常
nwind_comm.Cancel();
//关闭数据源链接
nwind_conn.Close();
}
}
}