using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;namespace StudentDAL.Operator
{
public class DataAccess
{
private SqlConnection objSqlConnection;
private SqlCommand objSqlCommand;
private SqlDataAdapter objSqlDataAdapter;
public DataAccess()
{
//连接数据库
string con="";
try
{
con=ConfigurationManager.ConnectionStrings["sql2000"].ConnectionString;
objSqlConnection=new SqlConnection(con);
objSqlCommand=new SqlCommand("",objSqlConnection);
objSqlDataAdapter=new SqlDataAdapter(objSqlCommand); }catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
//使用事物执行对学员信息表和成绩表的数据删除
public bool Del(int id)
{ try
{
objSqlConnection.Open();
//开始事物
objSqlTransaction = objSqlConnection.BeginTransaction();
objSqlCommand.Connection = objSqlConnection;
objSqlCommand.Transaction = objSqlTransaction;
objSqlCommand.CommandText = "delete from Student where StudentId=" + id +"";
objSqlCommand.ExecuteNonQuery();
objSqlCommand.CommandText = "delete from Score where StudentId=" + id + "";
objSqlCommand.ExecuteNonQuery();
objSqlTransaction.Commit();
return true; }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
objSqlTransaction.Rollback();
}
finally
{
objSqlConnection.Close();
}
return false;
}
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;namespace StudentDAL.Operator
{
public class DataAccess
{
private SqlConnection objSqlConnection;
private SqlCommand objSqlCommand;
private SqlDataAdapter objSqlDataAdapter;
public DataAccess()
{
//连接数据库
string con="";
try
{
con=ConfigurationManager.ConnectionStrings["sql2000"].ConnectionString;
objSqlConnection=new SqlConnection(con);
objSqlCommand=new SqlCommand("",objSqlConnection);
objSqlDataAdapter=new SqlDataAdapter(objSqlCommand); }catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
//使用事物执行对学员信息表和成绩表的数据删除
public bool Del(int id)
{ try
{
objSqlConnection.Open();
//开始事物
objSqlTransaction = objSqlConnection.BeginTransaction();
objSqlCommand.Connection = objSqlConnection;
objSqlCommand.Transaction = objSqlTransaction;
objSqlCommand.CommandText = "delete from Student where StudentId=" + id +"";
objSqlCommand.ExecuteNonQuery();
objSqlCommand.CommandText = "delete from Score where StudentId=" + id + "";
objSqlCommand.ExecuteNonQuery();
objSqlTransaction.Commit();
return true; }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
objSqlTransaction.Rollback();
}
finally
{
objSqlConnection.Close();
}
return false;
}
解决方案 »
- ASP.NET 执行存储过程 没有返回集合 输出ds.tables[0].rows[0][0]为空!
- 请教,我用webBrowser1调用网页,如何屏蔽网页中的关闭窗口按钮?
- 请大家帮忙解析以下List IList ArrayList的异同
- 问个键盘的
- C#国际化问题--急
- ListView虚拟模式下的问题
- 用c#程序时时从oacle倒到sqlserver2000里的数据为和出现乱码?
- 关于新增的记录,且返回当前新增的ID号
- 请问大家关于代理的问题!
- UDP问题
- 我的XP SP2无法安装VS2005,提示未能安装NET Famework 2.0, Error code 1603
- C#索引器中的异常
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; namespace StudentDAL.Operator
{
public class DataAccess
{
private SqlConnection objSqlConnection;
private SqlCommand objSqlCommand;
private SqlDataAdapter objSqlDataAdapter;
public DataAccess()
{
//连接数据库
string con="";
try
{
con=ConfigurationManager.ConnectionStrings["sql2000"].ConnectionString;
objSqlConnection=new SqlConnection(con);
objSqlCommand=new SqlCommand("",objSqlConnection);
objSqlDataAdapter=new SqlDataAdapter(objSqlCommand); }catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
//使用事物执行对学员信息表和成绩表的数据删除
public bool Del(int id)
{ try
{
objSqlConnection.Open();
//开始事物
objSqlTransaction = objSqlConnection.BeginTransaction();
objSqlCommand.Connection = objSqlConnection;
objSqlCommand.Transaction = objSqlTransaction;
objSqlCommand.CommandText = "delete from Student where StudentId=" + id +" delete from Score where StudentId=" + id + "";
objSqlCommand.ExecuteNonQuery();
objSqlTransaction.Commit();
return true; }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
objSqlTransaction.Rollback();
}
finally
{
objSqlConnection.Close();
}
return false;
}
//开始事物
objSqlCommand.Connection = objSqlConnection;
SqlTransaction tran=objSqlCommand.Transaction.BeginTransaction();
objSqlCommand.CommandText = "delete from Student where StudentId=" + id +"";
objSqlCommand.ExecuteNonQuery();
objSqlCommand.CommandText = "delete from Score where StudentId=" + id + "";
objSqlCommand.ExecuteNonQuery();
tran.Commit();
return true;
点都点不出的
不好意思。好久没用了。忘记了,应该是connection中的BeginTransaction