回答

收藏

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

技术问答 技术问答 332 人阅读 | 0 人回复 | 2023-09-11

下面给出了如何在我的数据库文件代码中增加totaldownloads值
* \% D7 o" p9 D2 U0 CSqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());. }4 l' J$ \+ z& L, Z
    SqlCommand sqlcmd = new SqlCommand();$ f8 s9 a) |: D
    SqlDataAdapter da = new SqlDataAdapter();* K/ V$ E7 j2 F1 n1 N
    DataTable dt = new DataTable();
0 ^1 o) q' O% d8 z3 r2 h0 B    DataRow dr;
2 B2 k+ y* o* u  S; i    protected void Page_Load(object sender, EventArgs e)
) q3 A# A6 `7 t* J/ c    {  r' V, i2 x1 M) F8 V7 }
        if (Session["UserId"] == null)0 d& W8 Y; q" M; S+ B* {
        {
. T5 z. Y1 ^( l            lblMessage.Visible = true;
) P4 ~% \! Z2 I/ \+ L& r            GridView1.Visible = false;
- e+ J: E+ C6 u7 `3 _            //AppContent.Visible = false;- ]# m7 j; J3 G/ @0 Q1 [7 ?6 Z- Q
        }* N$ l' |. J1 j; h' M7 R
        else: b5 a; |5 p' b5 Y/ T( p( A
        {! N# u* h" L( c5 ?2 w% D, w
            if (!Page.IsPostBack)* J, s+ U! g" f* g2 l' P
            {' ~0 B+ k& |0 }- W
                SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());+ k, M, ]3 H8 k- T. T2 p% s
                ArrayList myArrayList = ConvertDataSetToArrayList();" f3 I8 i# w/ Y3 [/ M& a
                Literal objliteral = new Literal();
7 g% E5 ^  B5 Z0 }6 ]                StringBuilder objSBuilder = new StringBuilder();' z+ {9 {" a7 }5 `7 f% o5 K
                //Add some column to datatable display some products           
" V  m( [6 v: l+ ?" t& r                dt.Columns.Add("appImg");
) l, W" M- c% h& |                dt.Columns.Add("appName");: k& R) w  M* j' a. L
                dt.Columns.Add("appLink");
7 n5 n+ ]4 f$ q. B3 V                dt.Columns.Add("appType");  S6 o! a0 G& O* |
                dt.Columns.Add("TotalVisitors");, K8 B7 n, R; o8 e) x
                dt.Columns.Add("TotalDownloads");# `: |* e, }  n0 a5 E% A( j
                dt.Columns.Add("RemainingVisitors");! d1 H. u: P8 N
                // Display each item of ArrayList
' d% b7 ]6 r6 F/ ]/ q2 v5 \                foreach (Object row in myArrayList)
; P; ~% e1 i: z& l0 I& r' D, J                {8 N/ O$ y6 R9 F' n' H' i* T
                    //Add rows with datatable and bind in the grid view
$ S$ f# A0 v5 I# D+ f0 F                    dr = dt.NewRow();$ A  }9 i7 k% L$ |) ~+ b
                    dr["appImg"] = ((DataRow)row)["AppImg"].ToString();
+ m/ S6 S3 p3 P  _3 Q) \                    dr["appName"] = ((DataRow)row)["AppName"].ToString();
2 J) {4 q2 g  d# r% a2 b2 B                    dr["appLink"] = ((DataRow)row)["AppLink"].ToString();$ t) y) z# m0 k# ~& ?" D
                    dr["appType"] = ((DataRow)row)["AppType"].ToString();2 ~; E7 z. ]) Z+ s
                    dr["TotalVisitors"] = &quotlan Visitors: " + ((DataRow)row)["TotalVisitors"].ToString();
) }7 X6 E5 D/ j) _1 K+ j1 \                    dr["TotalDownloads"] = "Downloaded: " + ((DataRow)row)["TotalDownloads"].ToString();. m  B( Y& E) n
                    dr["RemainingVisitors"] = "Remaining Visitors: " + ((DataRow)row)["RemainingVisitors"].ToString();7 x" [5 O6 Z6 O1 P3 K
                    dt.Rows.Add(dr);
$ o' g0 x$ P6 W                }
; P/ z8 W: @" e' D5 {5 K                GridView1.DataSource = dt;. I, I4 |5 b# ]% W, ~/ [/ a, i# n
                GridView1.DataBind();6 ]7 N8 K" j& i/ E
            }            8 `( S2 y6 P% o
        }
' T- a0 Y& Y& u! N9 V' A+ |! c& w    }
  O) L# h# y& E! t  S' }) R) z    public ArrayList ConvertDataSetToArrayList()
& D/ v' D, v, @; ]" @4 H$ D8 j    {* L1 K" g- R. T" u; c) l+ a
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());, l. R6 f& l9 k6 ]+ q6 U0 z
        SqlCommand cmd = new SqlCommand();3 C9 d3 H; w' p
        if (Session["UserId"] != null && (Session["UserTypeId"] != null && Convert.ToInt32(Session["UserTypeId"]) != 2))& L' E3 u7 V: f
        {* x# q; T# n" y/ |5 R" K
            cmd.CommandText = "Select * from tblApp WHERE AppType = '" + Session["UserOSType"] + "' ORDER BY TotalVisitors";* U9 ~. q3 d1 d( M6 d( i
        }
1 ~0 G$ ]1 G8 _( Q: i9 r: ?6 K        else
! k+ p% C6 ], e# H# [. Z* A        {* m. k/ i6 q* d3 ^
            cmd.CommandText = "Select * from tblApp ORDER BY TotalVisitors";            + ^& p% z) }$ W1 m. j4 N) N3 V1 o0 S
        }) _) J3 C- A0 H  I" Y+ {" ^, C8 z
        cmd.Connection = sqlcon;# ]6 Q% n( }8 I) V
        sqlcon.Open();
0 P6 v. o! [, n7 u+ T        cmd.ExecuteNonQuery();$ G' k# Y' e! @# @& D# m
        SqlDataAdapter da = new SqlDataAdapter();
( D. F( L* T6 }5 I        da.SelectCommand = cmd;
( E) Q* N2 v  e( D" j+ ^        DataSet dsApp = new DataSet();
% }/ Y5 X. D) w7 r# F1 C        da.Fill(dsApp, "tblApp");+ @5 @- e# M$ @* S7 U! a" }/ Z! T: _# q
        ArrayList myArrayList = new ArrayList();
/ |- l5 T2 I2 v! t        foreach (DataRow dtRow in dsApp.Tables[0].Rows)
  z* k8 N. D5 E# T        {8 `0 w1 C2 F) V, E
            myArrayList.Add(dtRow);
5 T, R( Q' |& f  b& \9 A' A        }
3 Z7 h) ?( X' _# Z; J( B        sqlcon.Close();1 s: b/ v7 M: ~$ h- n1 n8 X
        return myArrayList;# k7 B* w% V& h4 n: B0 }
    }
5 N6 O* X% {* y4 S    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e); f+ K; {: L- a+ E- y! m
    {  V& y* x4 {, ?9 Q7 Y5 W( T* f
        if (e.CommandName == "download")  Y* e5 N2 T3 e$ y5 R
        {
" ?1 n  }" w/ w0 z; t: d3 \            Button ib = (Button)e.CommandSource;) W& h8 a  H& w3 e
            int index = Convert.ToInt32(ib.CommandArgument);
- x6 D* G' }# n9 S            GridViewRow row = GridView1.Rows[index];            
( b) G! a  U* J  O1 Y            Label l2 = (Label)row.FindControl("Label2");- [" c/ s; w/ b+ x
            Label lbTotallVisitors = (Label)row.FindControl("Label4");5 q: K, a6 }3 ^0 L$ @; B' j
            Label lblTotalDownloads = (Label)row.FindControl("Label5");1 ^8 l9 b% \3 W1 Z& a5 K% p" b/ r
            Label lblRemainingVisitors = (Label)row.FindControl("Label6");0 H# ^9 m$ }* J  q

! x/ Z% `3 _: l& q4 {3 ?            string updateSQL = "UPDATE tblUser SET DownloadedApps = '" + lbl + "', Amount = '" + Session["Amount"] + "', TotalAmount='" + Session["TotalAmount"] + "' WHERE Id= '" + Session["UserId"] + "'";3 }: @$ L8 i* I- q5 M: n, q, k
            using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString()))
% u  t8 f: R4 f            {4 P& x% w0 e/ ?0 ^
                using (SqlCommand updateCommand = new SqlCommand(updateSQL, sqlConn))
: |2 a/ U. ?6 m8 D1 c                {/ S* r: X, c$ l
                    sqlConn.Open();
4 \; T5 Z/ R4 ~3 l8 O  F                    updateCommand.ExecuteNonQuery();
% a& ^! M$ G: b/ t                    updateCommand.Connection.Close();
8 C- E& j. m( t  H0 L                }* [) D& ~. D( f( l
            }            # ^6 ]% }( |/ R% k( ^" F
            Response.Redirect(l2.Text);. N& d4 p4 P( K9 ?- b3 m: T
        }
1 a( x- T/ ^% I! H& w/ n8 A, C) M    }! x, }9 \, ]" D. s9 n: v3 i
               
( G* y" |" `! G/ o4 [) d" E9 M2 o解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则