回答

收藏

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

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

下面给出了如何在我的数据库文件代码中增加totaldownloads值7 s- M0 y5 g1 E
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
$ v0 u1 E% v1 E3 b# V) u/ n) ^2 u! L    SqlCommand sqlcmd = new SqlCommand();
: h0 g/ C0 H3 e+ P4 y, t+ i4 p" ]    SqlDataAdapter da = new SqlDataAdapter();
) h3 G1 l7 B: K( O  Z    DataTable dt = new DataTable();4 U' A0 l) B  j' }! l# R+ C
    DataRow dr;
+ g. R4 p% f8 P  c2 {    protected void Page_Load(object sender, EventArgs e)0 E1 R/ c/ {7 ?( T
    {
! ]6 E0 ~' J& z* v2 M' f  ?/ m        if (Session["UserId"] == null)
9 D7 f! z; Y0 N, g; Y# z) u        {4 a! L" z5 Q; N! }3 P
            lblMessage.Visible = true;1 v3 T! a% N, P' b/ j
            GridView1.Visible = false;1 }5 p4 k0 Z7 L4 z3 X
            //AppContent.Visible = false;
4 X  ]. `2 v$ ~# d        }
' F2 G3 F  Z: R! C4 E1 D" ?        else: b/ r; L' ]1 N& `7 @
        {
9 u1 q% G& x0 f) i+ a; A, D            if (!Page.IsPostBack)
+ R9 _1 s! h7 m7 C8 B- P, s) x            {
. y4 ]  N% }/ s6 q6 r+ R) g                SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());! O$ p: X7 b% q$ T: z
                ArrayList myArrayList = ConvertDataSetToArrayList();
( a+ o% w8 u( O! M- ]; d1 [                Literal objliteral = new Literal();
8 p' s+ i) h  g3 i- o; S+ o                StringBuilder objSBuilder = new StringBuilder();
) B% v6 v2 I: A% W2 q, N                //Add some column to datatable display some products           
7 b- H3 t$ _+ u( m8 S) G5 ~3 @* {" G                dt.Columns.Add("appImg");6 X$ b+ B6 I! v0 i! w
                dt.Columns.Add("appName");
1 k( K6 \" t( ?, N, Z                dt.Columns.Add("appLink");
2 D& N0 {! l; M1 d5 e0 c1 Q- m+ D                dt.Columns.Add("appType");
* q9 V9 J2 W* E$ u- H& O$ S                dt.Columns.Add("TotalVisitors");4 z' N3 i4 K+ V' o; @" K6 F0 ]
                dt.Columns.Add("TotalDownloads");7 M* j; ~+ v) L& Z0 {4 m# B3 R
                dt.Columns.Add("RemainingVisitors");: C1 P9 h. T# a+ `6 K4 E+ k
                // Display each item of ArrayList
& X# y  P: l' k0 M                foreach (Object row in myArrayList)& a' U: n& l/ [2 M  [
                {- X: m+ Y5 Z- l
                    //Add rows with datatable and bind in the grid view
& _( s& s+ W% H1 L1 a                    dr = dt.NewRow();
' |5 z+ M& G( M4 ?0 Y* z0 N: ^                    dr["appImg"] = ((DataRow)row)["AppImg"].ToString();
: B9 I. X' W3 w8 Q9 @  q                    dr["appName"] = ((DataRow)row)["AppName"].ToString();
# @: }8 e- l( K( j" [/ y' o6 I                    dr["appLink"] = ((DataRow)row)["AppLink"].ToString();
4 O( Z# N3 G: O5 }                    dr["appType"] = ((DataRow)row)["AppType"].ToString();: F  I- D/ [; \5 z( i$ x$ Q
                    dr["TotalVisitors"] = &quotlan Visitors: " + ((DataRow)row)["TotalVisitors"].ToString();
7 `  U5 a; V. _                    dr["TotalDownloads"] = "Downloaded: " + ((DataRow)row)["TotalDownloads"].ToString();; _2 o0 V! S/ ^3 X. A! X
                    dr["RemainingVisitors"] = "Remaining Visitors: " + ((DataRow)row)["RemainingVisitors"].ToString();
2 K# _. v' k9 A& H                    dt.Rows.Add(dr);3 i2 o7 `4 G# V9 n1 b
                }
  E; q" ]; D# h! i3 ?  B, c: N                GridView1.DataSource = dt;; k  V- k) T2 r3 f$ N. `
                GridView1.DataBind();
) g& _$ L* _( b% l3 D; W: d            }            $ n% o2 s2 S' j( J% i
        }" Q* ~7 n1 j, U5 y6 n
    }
; t- w' k* N; Y; ?. D    public ArrayList ConvertDataSetToArrayList()# F! i6 |) m1 T  b+ m' [
    {
  Q  Z1 i0 D7 P$ T& p: r        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());3 u' e* s9 q7 ~, b
        SqlCommand cmd = new SqlCommand();
4 f8 B2 Y" G0 K1 U        if (Session["UserId"] != null && (Session["UserTypeId"] != null && Convert.ToInt32(Session["UserTypeId"]) != 2))
4 D9 L* P! J# i8 L; A1 p  u        {
& K" G: t5 }9 c5 ]6 O* E% ~+ e$ K            cmd.CommandText = "Select * from tblApp WHERE AppType = '" + Session["UserOSType"] + "' ORDER BY TotalVisitors";
% \  f1 d1 \+ `; q  a        }
' q9 a2 r( L. r# {' ?0 W$ o        else8 W7 K0 N" }- O# p
        {
0 `& l% J; \1 N3 t# Z6 L% a            cmd.CommandText = "Select * from tblApp ORDER BY TotalVisitors";            * c0 S$ A7 z  D4 ?, L4 Z' r2 [+ ^
        }$ g& G- w$ |* Y# _2 C
        cmd.Connection = sqlcon;
( w# o  g- F' T/ \        sqlcon.Open();' [, k6 o; y/ Z' a  l+ Z
        cmd.ExecuteNonQuery();" u' g: M6 n  j9 {4 O) L. S6 u
        SqlDataAdapter da = new SqlDataAdapter();5 F3 K/ X/ ?/ p! p0 j2 D
        da.SelectCommand = cmd;
( y4 y1 S3 [, l; ~- J) p4 G6 q        DataSet dsApp = new DataSet();* w* o) E2 F$ |- F1 A) L8 v, A
        da.Fill(dsApp, "tblApp");; s! A5 A* O, E& L3 }* \
        ArrayList myArrayList = new ArrayList();
; h9 B, k8 H* j1 C2 L        foreach (DataRow dtRow in dsApp.Tables[0].Rows)" x( U; t0 a$ k* X: ^
        {
$ E& s( V* r' ~( H- @' P9 C            myArrayList.Add(dtRow);. @6 c( C9 ]3 @! {
        }1 o" j$ H4 d4 R! l
        sqlcon.Close();
1 b7 C8 I$ Z2 d1 X# R- z  @        return myArrayList;
: @$ y% S6 x2 Y7 N" h: D    }
+ {2 |6 E+ W' P9 B- Q: o+ e  q5 X    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)9 J. P) T2 E# B5 y; T) `/ M
    {: f" {; g* f& X- v0 D7 R
        if (e.CommandName == "download"); M8 h# |/ N) B( X5 u
        {! w* _) p( u: v/ u5 A
            Button ib = (Button)e.CommandSource;
/ \5 R3 S0 {7 C. P2 H3 X2 O            int index = Convert.ToInt32(ib.CommandArgument);
$ Z& U# m/ z3 T- q8 h3 u            GridViewRow row = GridView1.Rows[index];            0 q4 b$ `2 H' j' t9 s: P* ]
            Label l2 = (Label)row.FindControl("Label2");- o4 ?  i4 J0 i6 K% {7 _) f
            Label lbTotallVisitors = (Label)row.FindControl("Label4");0 X3 d: h, X! {6 k7 k" B" z
            Label lblTotalDownloads = (Label)row.FindControl("Label5");
, T; v8 H$ K  R4 m1 G! G; I5 h            Label lblRemainingVisitors = (Label)row.FindControl("Label6");% g6 N8 b0 l5 H/ x5 w
& G& Q6 Y" K! T+ g" t. B/ x
            string updateSQL = "UPDATE tblUser SET DownloadedApps = '" + lbl + "', Amount = '" + Session["Amount"] + "', TotalAmount='" + Session["TotalAmount"] + "' WHERE Id= '" + Session["UserId"] + "'";
3 |2 D1 Q' ?4 I, |& ^( E            using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString()))0 ]5 ^# b, J3 A3 ~2 Q0 o3 k
            {3 ?, a" ~/ u5 y# S7 h
                using (SqlCommand updateCommand = new SqlCommand(updateSQL, sqlConn))
& O2 {) r6 k+ g4 T3 O                {( {, a1 L0 F  y, w$ X$ S* ~6 h
                    sqlConn.Open();! ]) s* Y2 q3 `. ?  \
                    updateCommand.ExecuteNonQuery();7 ]6 U9 p  j% Q# k6 u9 x0 t
                    updateCommand.Connection.Close();, u+ V! k" g- \$ A! c
                }+ E! V3 L: m: c5 J
            }            % B" R# P  _4 _) e* G. n2 i% \$ u
            Response.Redirect(l2.Text);
7 d$ K" b; J/ R7 X5 ]0 e% S        }7 E2 E* e) [" L% y* T
    }/ U9 f8 Z: D: a( h  U" @
                . O( }- X2 @& U% r. H  h
解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则