admin

使用c#asp.net在数据库中增加列值

sql

下面给出了如何在我的数据库文件代码中增加totaldownloads值

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);
        }
    }

阅读 160

收藏
2021-07-01

共1个答案

admin

更新表SET列=列+ 1

2021-07-01