使用C#asp.net数据库增量列值 [英] Increment Column Value in database using c# asp.net
本文介绍了使用C#asp.net数据库增量列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何提高我的数据库文件code totaldownloads值低于
给出 SqlConnection的sqlcon =新的SqlConnection(ConfigurationManager.ConnectionStrings [精读]的ToString());
SQLCMD的SqlCommand =新的SqlCommand();
SqlDataAdapter的大=新的SqlDataAdapter();
DataTable的DT =新的DataTable();
DataRow的博士; 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
如果(会话[用户ID] == NULL)
{
lblMessage.Visible = TRUE;
GridView1.Visible = FALSE;
//AppContent.Visible = FALSE;
}
其他
{
如果(!Page.IsPostBack)
{
SqlConnection的sqlcon =新的SqlConnection(ConfigurationManager.ConnectionStrings [精读]的ToString());
ArrayList的myArrayList = ConvertDataSetToArrayList();
文字objliteral =新的文字();
StringBuilder的objSBuilder =新的StringBuilder(); //一些列添加到数据表中显示一些产品
dt.Columns.Add(appImg);
dt.Columns.Add(APPNAME);
dt.Columns.Add(appLink);
dt.Columns.Add(的appType);
dt.Columns.Add(TotalVisitors);
dt.Columns.Add(TotalDownloads);
dt.Columns.Add(RemainingVisitors); //显示ArrayList的每个项目
的foreach(在myArrayList对象行)
{
//用数据表和绑定在网格视图中添加行
博士= dt.NewRow();
博士[appImg] =((DataRow中)行)[AppImg]的ToString()。
博士[APPNAME] =((DataRow中)行)[APPNAME]的ToString()。
博士[appLink] =((DataRow中)行)[AppLink]的ToString()。
博士[的appType] =((DataRow中)行)[的AppType,]的ToString()。
博士[TotalVisitors] =规划访问者:+((DataRow中)行)[TotalVisitors]的ToString();
博士[TotalDownloads] =下载:+((DataRow中)行)[TotalDownloads]的ToString();
博士[RemainingVisitors] =剩余访问者:+((DataRow中)行)[RemainingVisitors]的ToString();
dt.Rows.Add(DR);
} GridView1.DataSource = DT;
GridView1.DataBind();
}
}
} 公众的ArrayList ConvertDataSetToArrayList()
{
SqlConnection的sqlcon =新的SqlConnection(ConfigurationManager.ConnectionStrings [精读]的ToString());
CMD的SqlCommand =新的SqlCommand();
如果(会话[用户ID] = NULL&放大器;!及(会话[UserTypeId] = NULL&放大器;!&安培;!Convert.ToInt32(会话[UserTypeId])= 2))
{
cmd.CommandText ='+会话[UserOSType] +ORDER BY TotalVisitors从tblApp WHERE =的AppType选择*';
}
其他
{
cmd.CommandText =从tblApp ORDER BY TotalVisitors选择*;
}
cmd.Connection = sqlcon;
sqlcon.Open(); cmd.ExecuteNonQuery();
SqlDataAdapter的大=新的SqlDataAdapter();
da.SelectCommand = CMD; 数据集dsApp =新的DataSet();
da.Fill(dsApptblApp); ArrayList的myArrayList =新的ArrayList();
的foreach(DataRow的dtRow在dsApp.Tables [0] .Rows)
{
myArrayList.Add(dtRow);
}
sqlcon.Close();
返回myArrayList;
} 保护无效GridView1_RowCommand(对象发件人,GridViewCommandEventArgs E)
{ 如果(e.CommandName ==下载)
{
按钮IB =(按钮)e.CommandSource;
INT指数= Convert.ToInt32(ib.CommandArgument);
GridViewRow行= GridView1.Rows [指数]
标签L2 =(标签)row.FindControl(Label2的);
标签lbTotallVisitors =(标签)row.FindControl(Label4);
标签lblTotalDownloads =(标签)row.FindControl(Label5);
标签lblRemainingVisitors =(标签)row.FindControl(Label6);
串即updateSQL =UPDATE tblUser SET DownloadedApps ='+ LBL +',数量='+会话[数量] +',总金额='+会话[总金额] +'其中id =' +会话[用户ID] +';
使用(SqlConnection的sqlConn =新的SqlConnection(ConfigurationManager.ConnectionStrings [精读]。的ToString()))
{
使用(的SqlCommand的UpdateCommand =新的SqlCommand(即updateSQL,sqlConn))
{
sqlConn.Open();
updateCommand.ExecuteNonQuery();
updateCommand.Connection.Close(); }
}
的Response.Redirect(l2.Text);
}
}
解决方案
更新表SET列=栏+ 1
How to increase totaldownloads value in my database file code is given below
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserId"] == null)
{
lblMessage.Visible = true;
GridView1.Visible = false;
//AppContent.Visible = false;
}
else
{
if (!Page.IsPostBack)
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
ArrayList myArrayList = ConvertDataSetToArrayList();
Literal objliteral = new Literal();
StringBuilder objSBuilder = new StringBuilder();
//Add some column to datatable display some products
dt.Columns.Add("appImg");
dt.Columns.Add("appName");
dt.Columns.Add("appLink");
dt.Columns.Add("appType");
dt.Columns.Add("TotalVisitors");
dt.Columns.Add("TotalDownloads");
dt.Columns.Add("RemainingVisitors");
// Display each item of ArrayList
foreach (Object row in myArrayList)
{
//Add rows with datatable and bind in the grid view
dr = dt.NewRow();
dr["appImg"] = ((DataRow)row)["AppImg"].ToString();
dr["appName"] = ((DataRow)row)["AppName"].ToString();
dr["appLink"] = ((DataRow)row)["AppLink"].ToString();
dr["appType"] = ((DataRow)row)["AppType"].ToString();
dr["TotalVisitors"] = "Plan Visitors: " + ((DataRow)row)["TotalVisitors"].ToString();
dr["TotalDownloads"] = "Downloaded: " + ((DataRow)row)["TotalDownloads"].ToString();
dr["RemainingVisitors"] = "Remaining Visitors: " + ((DataRow)row)["RemainingVisitors"].ToString();
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
public ArrayList ConvertDataSetToArrayList()
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
SqlCommand cmd = new SqlCommand();
if (Session["UserId"] != null && (Session["UserTypeId"] != null && Convert.ToInt32(Session["UserTypeId"]) != 2))
{
cmd.CommandText = "Select * from tblApp WHERE AppType = '" + Session["UserOSType"] + "' ORDER BY TotalVisitors";
}
else
{
cmd.CommandText = "Select * from tblApp ORDER BY TotalVisitors";
}
cmd.Connection = sqlcon;
sqlcon.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet dsApp = new DataSet();
da.Fill(dsApp, "tblApp");
ArrayList myArrayList = new ArrayList();
foreach (DataRow dtRow in dsApp.Tables[0].Rows)
{
myArrayList.Add(dtRow);
}
sqlcon.Close();
return myArrayList;
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "download")
{
Button ib = (Button)e.CommandSource;
int index = Convert.ToInt32(ib.CommandArgument);
GridViewRow row = GridView1.Rows[index];
Label l2 = (Label)row.FindControl("Label2");
Label lbTotallVisitors = (Label)row.FindControl("Label4");
Label lblTotalDownloads = (Label)row.FindControl("Label5");
Label lblRemainingVisitors = (Label)row.FindControl("Label6");
string updateSQL = "UPDATE tblUser SET DownloadedApps = '" + lbl + "', Amount = '" + Session["Amount"] + "', TotalAmount='" + Session["TotalAmount"] + "' WHERE Id= '" + Session["UserId"] + "'";
using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString()))
{
using (SqlCommand updateCommand = new SqlCommand(updateSQL, sqlConn))
{
sqlConn.Open();
updateCommand.ExecuteNonQuery();
updateCommand.Connection.Close();
}
}
Response.Redirect(l2.Text);
}
}
解决方案
UPDATE Table SET Column = Column + 1
这篇关于使用C#asp.net数据库增量列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文