回答

收藏

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

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

下面给出了如何在我的数据库文件代码中增加totaldownloads值" M) g0 _/ F% `& ~1 o  ?% D  V' l, i
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
, H8 T' C* i( b* P$ y- U    SqlCommand sqlcmd = new SqlCommand();1 v& K, S6 \3 w8 K$ q$ Z$ S  m1 `6 B
    SqlDataAdapter da = new SqlDataAdapter();; W, t  I6 {+ u8 D7 v! X5 _5 c( [, `
    DataTable dt = new DataTable();
& Y) L' E2 Y, h: q  M( L    DataRow dr;. I: U) L, @; X4 a1 W& r  j( t: p
    protected void Page_Load(object sender, EventArgs e)
3 q4 d7 e/ p7 P: C    {
/ O% k! }- P) [9 |        if (Session["UserId"] == null)7 P: `3 i7 i; l) _
        {8 t) Q+ _5 K% l0 w+ A  ~; o8 H1 ?
            lblMessage.Visible = true;9 R% k6 Q, @$ T! ~
            GridView1.Visible = false;
/ E1 R! `: q. C( Q+ k            //AppContent.Visible = false;
* S* }% _5 {. S( V4 c' W        }2 x0 S3 `* S* j+ `! x! S- X
        else
! \! ?( I1 d+ |  s        {+ c; x$ D* \7 x( c1 g2 y% j
            if (!Page.IsPostBack): y5 L9 o& S& y
            {
# `( ^+ S6 \6 u4 y                SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
/ P# G0 s+ q3 B% G$ W4 Z* C                ArrayList myArrayList = ConvertDataSetToArrayList();: s* _3 i) ~, @0 P! w
                Literal objliteral = new Literal();
% K7 R. {3 X5 S+ U! }# G                StringBuilder objSBuilder = new StringBuilder();
8 C2 o5 f" j8 H/ ?+ F! Z$ m7 f/ E3 Q                //Add some column to datatable display some products           ; v$ Y' r3 q" t: b. }* e
                dt.Columns.Add("appImg");
$ _: O8 M1 m9 ^# u1 i# z                dt.Columns.Add("appName");, K( M0 c! G4 u, [* v" ]% o0 R: R
                dt.Columns.Add("appLink");- i3 d4 f/ d! M* x
                dt.Columns.Add("appType");5 e  |7 i  W  x8 H
                dt.Columns.Add("TotalVisitors");
' y; _8 N+ a. |0 Z' A                dt.Columns.Add("TotalDownloads");; }. g* i; S, p6 D8 l2 Q" w& C
                dt.Columns.Add("RemainingVisitors");! [2 Y$ Y; G7 E1 {6 }* K
                // Display each item of ArrayList) ]! R2 \: b& W: a7 A
                foreach (Object row in myArrayList)
3 ^5 U$ `8 ]% U: Z" q                {
) k& y$ A! D5 ~                    //Add rows with datatable and bind in the grid view& ~8 F$ y: r5 K* Z1 |$ v6 M
                    dr = dt.NewRow();/ W( {. Y+ X/ Z, I
                    dr["appImg"] = ((DataRow)row)["AppImg"].ToString();
, y- \% T8 }% c+ P* N5 j                    dr["appName"] = ((DataRow)row)["AppName"].ToString();
  Q. G: Q1 Q9 z( W/ L: A0 @                    dr["appLink"] = ((DataRow)row)["AppLink"].ToString();
: c% w: K/ K: y0 k  f. i; j                    dr["appType"] = ((DataRow)row)["AppType"].ToString();
& @+ p+ I2 E0 l3 d2 V8 t/ r7 s                    dr["TotalVisitors"] = &quotlan Visitors: " + ((DataRow)row)["TotalVisitors"].ToString();
, ?" B& R, d5 ?                    dr["TotalDownloads"] = "Downloaded: " + ((DataRow)row)["TotalDownloads"].ToString();
' H& w# {  w6 n. s& x6 F4 k6 {; S( b                    dr["RemainingVisitors"] = "Remaining Visitors: " + ((DataRow)row)["RemainingVisitors"].ToString();" c3 Q( I! _) B. i
                    dt.Rows.Add(dr);
! u/ N2 ~% j* e$ q: F# W& ]                }, T* G! w) {1 D" s4 R
                GridView1.DataSource = dt;2 |; M1 J: d' c0 `5 a- F1 g
                GridView1.DataBind();
2 x4 J" y# E& q) ^6 c2 E& N            }            
% q! f# k* K( T& @+ Q* R8 z2 L        }( o0 j. a# }  {0 L# L: ~& W
    }! c7 [8 ]  ?4 s% s6 z( J
    public ArrayList ConvertDataSetToArrayList()
/ q. f7 F4 ^; v9 E- G* o: k: r    {
4 {! x* ?. v" R( K        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());4 `- }! K9 U) ~) p: s5 o! O+ x
        SqlCommand cmd = new SqlCommand();
0 @' L" i5 r1 q" [        if (Session["UserId"] != null && (Session["UserTypeId"] != null && Convert.ToInt32(Session["UserTypeId"]) != 2))
5 U. |6 }+ j1 V        {0 {, V* v" e9 w& ^$ h
            cmd.CommandText = "Select * from tblApp WHERE AppType = '" + Session["UserOSType"] + "' ORDER BY TotalVisitors";
" I0 G3 \% z* Q( l: ~7 U        }
5 n! o6 n; ~1 T1 R3 n        else
6 ?- T7 k, l$ m        {8 g1 B; b1 R" z; F6 w8 Z
            cmd.CommandText = "Select * from tblApp ORDER BY TotalVisitors";            
/ U0 w# @. K. j: |! |        }  R; @/ v1 e; b6 [: d
        cmd.Connection = sqlcon;0 z8 R6 s. Z3 x) U9 b# p
        sqlcon.Open();% n( M% d2 @  m
        cmd.ExecuteNonQuery();5 ^! L4 P0 m6 n! T- S! I3 n+ |
        SqlDataAdapter da = new SqlDataAdapter();
6 D, V6 U# }* Q$ C+ I* a        da.SelectCommand = cmd;8 w' B2 @; @* S( O7 J: H
        DataSet dsApp = new DataSet();( a7 f! n4 Z4 F) L: e
        da.Fill(dsApp, "tblApp");
* I5 K' y9 q5 |6 L* m+ }        ArrayList myArrayList = new ArrayList();
- x8 g/ J. f. C: v        foreach (DataRow dtRow in dsApp.Tables[0].Rows)
% J4 C* ]% w; ^/ r6 r+ o) \, R        {
7 ]7 N# n/ V# @. Q! h5 ^            myArrayList.Add(dtRow);# Z  N, ~4 _4 _+ F" L
        }' ^! j% l+ u8 L6 w4 f
        sqlcon.Close();
& Y( ^. y) B; u4 {( A. @  R        return myArrayList;
5 O1 j" X6 ]! ~  s+ D# X: p# E5 D    }) W& Y) j2 b6 ?  Y) }5 B- u
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
/ H) y$ i8 f1 Q( j    {2 I/ N& ~! J- G$ E
        if (e.CommandName == "download")4 D4 W! u" z/ r5 s7 j$ j
        {
7 ^9 l: \6 K5 c* W" p            Button ib = (Button)e.CommandSource;
( Z9 P" H' n1 Y* C6 e' H0 g) P* s            int index = Convert.ToInt32(ib.CommandArgument);( e0 E% F" r1 B" Y- I8 }2 S
            GridViewRow row = GridView1.Rows[index];            : v1 E- O9 a) i# A  Z
            Label l2 = (Label)row.FindControl("Label2");
. [4 `. D0 M) T8 c2 y            Label lbTotallVisitors = (Label)row.FindControl("Label4");' n7 J# ^% O& m/ l# w% v; O! C
            Label lblTotalDownloads = (Label)row.FindControl("Label5");) V% ]" c8 b+ n
            Label lblRemainingVisitors = (Label)row.FindControl("Label6");
9 m+ R: T" k% p/ D
# J7 _  y4 m+ O            string updateSQL = "UPDATE tblUser SET DownloadedApps = '" + lbl + "', Amount = '" + Session["Amount"] + "', TotalAmount='" + Session["TotalAmount"] + "' WHERE Id= '" + Session["UserId"] + "'";
( u' J3 ]6 `) H/ V% a            using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString()))# a% r+ f* @# S* M- Z- y  U1 R3 t! n
            {* {& i# W  |- |, q7 |4 L. p
                using (SqlCommand updateCommand = new SqlCommand(updateSQL, sqlConn))! c+ Z% t5 C- t- d7 M4 `- `
                {% X+ Q; e* ?" @( }/ H7 l! ]% |
                    sqlConn.Open();! ]3 x# J% h. ?
                    updateCommand.ExecuteNonQuery();
& X. X% j3 ]8 }9 ^' Q8 l# _                    updateCommand.Connection.Close();# B( o4 |$ e$ S3 A) K5 m
                }
$ P  C" r8 e& g" r. v$ Y* g            }            & o2 Y* Y  A+ @, O2 R4 j. X  ~
            Response.Redirect(l2.Text);. g. Q  P* x* d1 d# d/ f" I. Y0 a* [
        }
$ G5 O- `& z8 Z: s/ l0 A    }
5 z+ x9 ?$ U5 q2 t               
1 f4 ]# r  F4 v! |8 I2 n( x解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则