Enterprise Library 2.0系列:添加一条记录,返回主键ID号(存储过程)

Enterprise Library 2.0系列:添加一条记录,返回主键ID号    -- 新建一篇日志,返回主键ID值。
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
    ALTER PROCEDURE dbo.BlogMainNew 
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    (
Enterprise Library 2.0系列:添加一条记录,返回主键ID号        
@title nvarchar (100),
Enterprise Library 2.0系列:添加一条记录,返回主键ID号        
@content text
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    )
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
AS    
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
Enterprise Library 2.0系列:添加一条记录,返回主键ID号        
Insert Into BlogMain (title, content) values (@title@content);
Enterprise Library 2.0系列:添加一条记录,返回主键ID号        
Select @@IDENTITY;
Enterprise Library 2.0系列:添加一条记录,返回主键ID号        
RETURN
Enterprise Library 2.0系列:添加一条记录,返回主键ID号

Enterprise Library 2.0系列:添加一条记录,返回主键ID号    Database db = DatabaseFactory.CreateDatabase();
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    DbCommand cmd 
= db.GetStoredProcCommand("BlogMainNew");
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    db.AddInParameter(cmd, 
"@title", DbType.String, this.tbxTitle.Text.Trim());
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    db.AddInParameter(cmd, 
"@content", DbType.String, this.tbxContent.Text.Trim());
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
object objID = db.ExecuteScalar(cmd); // ExecuteNonQuery 方法返回的是受影响的行数,不能混用了。
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
    Response.Write("<br>主键ID是:" + Convert.ToInt32(objID));
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    Response.End();
Enterprise Library 2.0系列:添加一条记录,返回主键ID号


Enterprise Library 2.0系列:添加一条记录,返回主键ID号(参数化SQL语句)

Enterprise Library 2.0系列:添加一条记录,返回主键ID号    string sName = "黄琴";
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
string sAddress = "IBM ThinkPad 制造";
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
string sCity = "深圳";
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
string sCountry = "中国";
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
string sPostalCode = "518000";
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
// 创建 Database、DbCommand 对象
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
    Database db = DatabaseFactory.CreateDatabase("CS_QuickStarts");
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
string strSql = "Insert Into Customers (Name, Address, City, Country, PostalCode) values (@Name, @Address, @City, @Country, @PostalCode); Select @@IDENTITY;";
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    DbCommand cmd 
= db.GetSqlStringCommand(strSql);
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
// 添加参数
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
    db.AddInParameter(cmd, "Name", DbType.String, sName);
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    db.AddInParameter(cmd, 
"Address", DbType.String, sAddress);
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    db.AddInParameter(cmd, 
"City", DbType.String, sCity);
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    db.AddInParameter(cmd, 
"Country", DbType.String, sCountry);
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    db.AddInParameter(cmd, 
"PostalCode", DbType.String, sPostalCode);
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
// 执行
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
    int identity = Convert.ToInt32(db.ExecuteScalar(cmd));
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    Response.Write(
"添加成功,该主键ID为:" + identity + "");
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    Response.End();
Enterprise Library 2.0系列:添加一条记录,返回主键ID号


Enterprise Library 2.0系列:添加一条记录,返回主键ID号(非参数化SQL语句)

Enterprise Library 2.0系列:添加一条记录,返回主键ID号    string strUserName = "王彩锦";
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
string strBirthday = "1982-08-21";
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
// 创建 Database 对象
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
    Database db = DatabaseFactory.CreateDatabase();
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
// 执行非参数的 SQL 语句
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
    string strSql = "Insert Into Users (UserName, Birthday) values ('"+strUserName+"', '"+strBirthday+"');";
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    strSql 
+= "Select @@IDENTITY;";
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
object identity = db.ExecuteScalar(CommandType.Text, strSql);
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    
// 显示结果
Enterprise Library 2.0系列:添加一条记录,返回主键ID号
    Response.Write("新建用户成功,该用户的ID是:" + identity);
Enterprise Library 2.0系列:添加一条记录,返回主键ID号    Response.End();

相关文章: