下面是主要代码,对数据进行增加,删除,更新,插入
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.Data.Common;
- using Microsoft.Practices.EnterpriseLibrary.Common;
- using Microsoft.Practices.EnterpriseLibrary.Data;
- namespace WebSite
- {
- public partial class MicrosoftData : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void btnSure_Click(object sender, EventArgs e)
- {
- // Create the Database object, using the default database service. The
- //default database service is determined through configuration.
- Database db = DatabaseFactory.CreateDatabase();
- DataSet productsDataSet = new DataSet();
- string sqlCommand = "Select * " +
- "From Users";
- DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
- string productsTable = "Users";
- // Retrieve the initial data
- db.LoadDataSet(dbCommand, productsDataSet, productsTable);
- // Get the table that will be modified
- DataTable table = productsDataSet.Tables[productsTable];
- GridView1.DataSource = table;
- GridView1.DataBind();
- // Establish our Insert, Delete, and Update commands
- DbCommand insertCommand = db.GetStoredProcCommand("addUsers");
- db.AddInParameter(insertCommand, "UserName", DbType.String, "f");
- db.AddInParameter(insertCommand, "Password", DbType.String, "f");
- db.ExecuteNonQuery(insertCommand);
- DbCommand update = db.GetSqlStringCommand("update users set userName=@userName,password=@password where userID=@userID");
- db.AddInParameter(update, "UserName", DbType.String, "fA");
- db.AddInParameter(update, "Password", DbType.String, "fA");
- db.AddInParameter(update, "UserID", DbType.Int32, 1053664);
- db.ExecuteNonQuery(update);
- DbCommand deleteCommand = db.GetStoredProcCommand("DeleteProduct");
- db.AddInParameter(deleteCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
- //DbCommand updateCommand = db.GetStoredProcCommand("UpdateProduct");
- //db.AddInParameter(updateCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
- //db.AddInParameter(updateCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
- //db.AddInParameter(updateCommand, "LastUpdate", DbType.DateTime, "LastUpdate", DataRowVersion.Current);
- //// Submit the DataSet, capturing the number of rows that were affected
- //int rowsAffected = db.UpdateDataSet(productsDataSet, "Products", insertCommand, updateCommand,
- // deleteCommand, UpdateBehavior.Standard);
- }
- }
- }