zoukankan      html  css  js  c++  java
  • Microsoft.Practices.EnterpriseLibrary.Data 数据库操作

    1. Database db = null;  
    2.        
    3.       #region 一般调用  
    4.       db = DatabaseFactory.CreateDatabase("Connection String");  
    5.       int count = (int)db.ExecuteScalar(CommandType.Text, "SELECT Count(*) From cms_company");  
    6.       string message = string.Format("There are {0} customers in the database", count.ToString());  
    7.       Response.Write(message);  
    8.       #endregion  
    9.  
    10.  
    11.       #region 带返回参数,返回值和返回数据集,一般参数的存储过程  
    12.       //CREATE PROCEDURE [dbo].[kword]  
    13.       //@kword varchar(250)='',  
    14.       //@top int,  
    15.       //@otop varchar(250) output  
    16.       //As  
    17.       //select top 10 * from Table1 where ntitle like '%'+@kword+'%' and id>@top  
    18.       //declare @flag int  
    19.       //select @flag=100  
    20.       //SET @otop='返回值'  
    21.       //return @flag  
    22.       db = DatabaseFactory.CreateDatabase("serverConnectionString");//连接字符串变量名  
    23.       DbCommand dbcomm = db.GetStoredProcCommand("kword");//存储过程名  
    24.       db.AddInParameter(dbcomm, "@kword", DbType.String, "创业");//参数名 类型 值  
    25.       db.AddInParameter(dbcomm, "top", DbType.Int32, 2);//参数名 类型 值  
    26.       db.AddOutParameter(dbcomm, "otop", DbType.String, 250);//output参数名 类型 长度  
    27.       //关键在这里,添加一个参数@RETURN_VALUE 类型为ReturnValue  
    28.       db.AddParameter(dbcomm, "@RETURN_VALUE", DbType.String, ParameterDirection.ReturnValue, "", DataRowVersion.Current, null);  
    29.       DataSet ds = db.ExecuteDataSet(dbcomm);//必须有执行的动作后面才能获取值  
    30.       //title = (string)db.ExecuteScalar(dbcomm);如果返回只有一个数据,这样也是可以的  
    31.       GridView1.DataSource = ds;  
    32.       GridView1.DataBind();  
    33.   
    34.       Response.Write("<br>output输出参数值:" + db.GetParameterValue(dbcomm, "otop").ToString());  
    35.       // int testvalue = (int)dbcomm.Parameters["@RETURN_VALUE"].Value; //另一种获取值的方式  
    36.       Response.Write("<br />return返回参数值:" + db.GetParameterValue(dbcomm, "RETURN_VALUE").ToString());  
    37.       #endregion  
    38.  
    39.  
    40.  
    41.       #region 使用事务记录操作数据库  
    42.       //CREATE TABLE [dbo].[Table1](  
    43.       //    [id] [int] IDENTITY(1,1) NOT NULL,  
    44.       //    [ntitle] [varchar](250) NOT NULL,  
    45.       //    [valuea] [varchar](250) NULL,  
    46.       // CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED   
    47.       //(  
    48.       //    [ntitle] ASC  
    49.       //)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]  
    50.       //) ON [PRIMARY]  
    51.       db = DatabaseFactory.CreateDatabase("serverConnectionString");  
    52.       using (IDbConnection conn = db.CreateConnection())  
    53.       {  
    54.           conn.Open();  
    55.           IDbTransaction _trans = conn.BeginTransaction();  
    56.           try  
    57.           {  
    58.               DbCommand _cmd = db.GetSqlStringCommand("INSERT INTO [Table1]([ntitle],[valuea]) VALUES(@ntitle,@valuea)");  
    59.               db.AddInParameter(_cmd, "ntitle", DbType.String, "AA");  
    60.               db.AddInParameter(_cmd, "valuea", DbType.String, "AA");  
    61.               db.ExecuteNonQuery(_cmd, _trans as DbTransaction);  
    62.               db.ExecuteNonQuery(_cmd, _trans as DbTransaction);//ntitle字段上建有唯一索引,故第二次插入同样记录时会报错  
    63.               _trans.Commit();  
    64.           }  
    65.           catch  
    66.           {  
    67.               try  
    68.               {  
    69.                   _trans.Rollback();//事务提交失败时,则回滚(是否回滚成功,可查看表中有无AA的记录即可)  
    70.               }  
    71.               catch { }  
    72.           }  
    73.           finally  
    74.           {  
    75.               conn.Close();  
    76.           }  
    77.       }  
    78.       #endregion 
  • 相关阅读:
    English,The Da Vinci Code, Chapter 23
    python,meatobject
    English,The Da Vinci Code, Chapter 22
    English,The Da Vinci Code, Chapter 21
    English,The Da Vinci Code, Chapter 20
    English,The Da Vinci Code, Chapter 19
    python,xml,ELement Tree
    English,The Da Vinci Code, Chapter 18
    English,The Da Vinci Code, Chapter 17
    English,The Da Vinci Code, Chapter 16
  • 原文地址:https://www.cnblogs.com/jordan2009/p/2020070.html
Copyright © 2011-2022 走看看