ADO.Net 浅析(五)-ADO对象之---连接对象

ADO与操作数据库,第一步是,创建连接对象,配置连接对象.

下面,对sqlconnection对象做一些介绍

(一)成员概览

BeginTransaction 已重载。 开始数据库事务。
ChangeDatabase 为打开的 SqlConnection 更改当前数据库。 (重写 DbConnection ..::. ChangeDatabase(String) 。)
GetSchema 已重载。 返回此 SqlConnection 的数据源的架构信息。

 

ConnectionTimeout 获取在尝试建立连接时终止尝试并生成错误之前所等待的时间。 (重写 DbConnection ..::. ConnectionTimeout 。)
Database 获取当前数据库或连接打开后要使用的数据库的名称。 (重写 DbConnection ..::. Database 。)
DataSource 获取要连接的 SQL Server 实例的名称。 (重写 DbConnection ..::. DataSource 。)
State 指示 SqlConnection 的状态。 (重写 DbConnection ..::. State 。)

 

 (1)BeginTransaction()

     public   class  MyTransaction
    {

        
private   string  connstr;
        
public   string  Connstr
        {
            
get  {  return  connstr; }
            
set  { connstr  =  value; }
        }

        
public  MyTransaction()
        {

            connstr 
=  ConfigurationManager.ConnectionStrings[ " AdWEntities " ].ConnectionString;
        }

        
public   void  Trans()
        {

            
using  (SqlConnection conn  =   new  SqlConnection(Connstr))
            {



                conn.Open();
                SqlTransaction trs 
=  conn.BeginTransaction();
                SqlCommand cmd 
=  conn.CreateCommand();
                cmd.CommandText 
=   " INSERT INTO [HumanResources].[Employee]  "   +
               
" ([BusinessEntityID]  "   +
              
"  ,[NationalIDNumber]  "   +
              
"  ,[LoginID]  "   +
              
"  ,[OrganizationNode]  "   +
              
"  ,[JobTitle]  "   +
              
"  ,[BirthDate]  "   +
              
"  ,[MaritalStatus]  "   +
              
" ,[Gender]  "   +
              
"  ,[HireDate]  "   +
              
"  ,[SalariedFlag]  "   +
              
"  ,[VacationHours]  "   +
              
"  ,[SickLeaveHours]  "   +
              
"  ,[CurrentFlag]  "   +
              
"  ,[rowguid]  "   +
              
"  ,[ModifiedDate])  "   +
        
"  VALUES  "   +
             
"   ('293'  "   +
             
"   ,'134219719'  "   +
             
"   ,'adventure-works\ranjit123'  "   +
             
"   ,'/'  "   +
             
"   ,'Sales Representative'  "   +
             
"   ,'1965-10-31'  "   +
             
"   ,'S'  "   +
             
"   ,'M' "   +
             
"   ,'2002-07-01'  "   +
             
"   ,'1'  "   +
             
"   ,'34'  "   +
             
"   ,'37'  "   +
             
"   ,'1'  "   +
             
"   ,NEWID()  "   +
             
"  ,'2004-07-31 00:00:00.000') " ;
                
try
                {
                    cmd.Transaction 
=  trs;

                    cmd.ExecuteNonQuery();
                    Console.WriteLine(
" 1 " );
                    cmd.ExecuteNonQuery();
                    Console.WriteLine(
" 2 " );
                    trs.Commit();
                }
                
catch  (Exception ex)
                {
                    Console.WriteLine(
" Commit Exception Type: {0} " , ex.GetType());
                    Console.WriteLine(
"   Message: {0} " , ex.Message);


                    trs.Rollback();
                }
                

            }

        }



    }

 

 

你可能感兴趣的:(.net)