Add/Delete/Update

首先是数据访问层的代码:

  1 None.gif using  System;
  2 None.gif using  System.Data;
  3 None.gif using  System.Data.SqlClient;
  4 None.gif using  System.Configuration;
  5 None.gif
  6 None.gif namespace  WebTest.Common
  7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  9InBlock.gif    /// COperator 的摘要说明。
 10ExpandedSubBlockEnd.gif    /// </summary>

 11InBlock.gif    public class COperator
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 13InBlock.gif        public COperator()
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 15InBlock.gif            //
 16InBlock.gif            // TODO: 在此处添加构造函数逻辑
 17InBlock.gif            //
 18ExpandedSubBlockEnd.gif        }

 19InBlock.gif        public DataSet HaveParameter(string StrProcedure)
 20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 21InBlock.gif            SqlConnection MyConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"]);
 22InBlock.gif            SqlCommand MyCommand = new SqlCommand();
 23InBlock.gif            MyCommand.Connection = MyConnection;
 24InBlock.gif            MyCommand.CommandType = CommandType.StoredProcedure;
 25InBlock.gif            MyCommand.CommandText = StrProcedure;
 26InBlock.gif
 27InBlock.gif
 28InBlock.gif            SqlDataAdapter MyAdapter = new SqlDataAdapter();
 29InBlock.gif            
 30InBlock.gif            MyAdapter.SelectCommand= MyCommand;
 31InBlock.gif
 32InBlock.gif            DataSet Ds = new DataSet();
 33InBlock.gif
 34InBlock.gif            if (MyConnection.State==System.Data.ConnectionState.Closed)
 35ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 36InBlock.gif                MyConnection.Open();
 37ExpandedSubBlockEnd.gif            }

 38InBlock.gif            // 启动一个事务
 39InBlock.gif            SqlTransaction MyTrans = MyConnection.BeginTransaction();
 40InBlock.gif
 41InBlock.gif            // initialize command object
 42InBlock.gif            MyCommand.Transaction = MyTrans;
 43InBlock.gif
 44InBlock.gif            try
 45ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 46InBlock.gif                // 进行数据库操作
 47InBlock.gif                MyAdapter.Fill(Ds);
 48InBlock.gif                MyTrans.Commit();
 49ExpandedSubBlockEnd.gif            }

 50InBlock.gif            catch
 51ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 52InBlock.gif                MyTrans.Rollback();
 53ExpandedSubBlockEnd.gif            }

 54InBlock.gif            finally
 55ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 56InBlock.gif                MyConnection.Close();
 57ExpandedSubBlockEnd.gif            }

 58InBlock.gif
 59InBlock.gif            return Ds;
 60ExpandedSubBlockEnd.gif        }

 61InBlock.gif
 62InBlock.gif        public void NoReturnFunction(string StrParameter, string StrField, string StrProcedure)
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 64InBlock.gif            // 创建数据库连接和命令的对象
 65InBlock.gif            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"]);
 66InBlock.gif            
 67InBlock.gif            SqlCommand myCommand = new SqlCommand(StrProcedure, myConnection);
 68InBlock.gif
 69InBlock.gif            // 指明Sql命令的操作类型是使用存储过程
 70InBlock.gif            myCommand.CommandType = CommandType.StoredProcedure;
 71InBlock.gif
 72InBlock.gif            SqlParameter parameterStr = new SqlParameter(StrField, SqlDbType.VarChar, 500);
 73InBlock.gif            parameterStr.Value = StrParameter;
 74InBlock.gif            myCommand.Parameters.Add(parameterStr);
 75InBlock.gif
 76InBlock.gif            // 打开数据库连接
 77InBlock.gif            if (myConnection.State==System.Data.ConnectionState.Closed)
 78ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 79InBlock.gif                myConnection.Open();
 80ExpandedSubBlockEnd.gif            }

 81InBlock.gif
 82InBlock.gif            // 启动一个事务
 83InBlock.gif            SqlTransaction MyTrans = myConnection.BeginTransaction();
 84InBlock.gif
 85InBlock.gif            // initialize command object
 86InBlock.gif            myCommand.Transaction = MyTrans;
 87InBlock.gif
 88InBlock.gif            try
 89ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 90InBlock.gif                // 进行数据库操作
 91InBlock.gif                myCommand.ExecuteNonQuery();
 92InBlock.gif                MyTrans.Commit();
 93ExpandedSubBlockEnd.gif            }

 94InBlock.gif            catch
 95ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 96InBlock.gif                MyTrans.Rollback();
 97ExpandedSubBlockEnd.gif            }

 98InBlock.gif            finally
 99ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
100InBlock.gif                // 关闭数据库连接
101InBlock.gif                myConnection.Close();
102ExpandedSubBlockEnd.gif            }
    
103ExpandedSubBlockEnd.gif        }

104InBlock.gif
105InBlock.gif        // 2参数
106InBlock.gif        public void NoReturnFunction(string StrParameter1, string StrField1, string StrParameter2, string StrField2, string StrProcedure)
107ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
108InBlock.gif            // 创建数据库连接和命令的对象
109InBlock.gif            SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"]);
110InBlock.gif            
111InBlock.gif            SqlCommand myCommand = new SqlCommand(StrProcedure, myConnection);
112InBlock.gif
113InBlock.gif            // 指明Sql命令的操作类型是使用存储过程
114InBlock.gif            myCommand.CommandType = CommandType.StoredProcedure;
115InBlock.gif
116InBlock.gif            SqlParameter parameterStr1 = new SqlParameter(StrField1, SqlDbType.NVarChar, 50);
117InBlock.gif            parameterStr1.Value = StrParameter1;
118InBlock.gif            myCommand.Parameters.Add(parameterStr1);
119InBlock.gif
120InBlock.gif            SqlParameter parameterStr2 = new SqlParameter(StrField2, SqlDbType.NVarChar, 50);
121InBlock.gif            parameterStr2.Value = StrParameter2;
122InBlock.gif            myCommand.Parameters.Add(parameterStr2);
123InBlock.gif
124InBlock.gif            // 打开数据库连接
125InBlock.gif            if (myConnection.State==System.Data.ConnectionState.Closed)
126ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
127InBlock.gif                myConnection.Open();
128ExpandedSubBlockEnd.gif            }

129InBlock.gif
130InBlock.gif            // 启动一个事务
131InBlock.gif            SqlTransaction MyTrans = myConnection.BeginTransaction();
132InBlock.gif
133InBlock.gif            // initialize command object
134InBlock.gif            myCommand.Transaction = MyTrans;
135InBlock.gif
136InBlock.gif            try
137ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
138InBlock.gif                // 进行数据库操作
139InBlock.gif                myCommand.ExecuteNonQuery();
140InBlock.gif                MyTrans.Commit();
141ExpandedSubBlockEnd.gif            }

142InBlock.gif            catch
143ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
144InBlock.gif                MyTrans.Rollback();
145ExpandedSubBlockEnd.gif            }

146InBlock.gif            finally
147ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
148InBlock.gif                // 关闭数据库连接
149InBlock.gif                myConnection.Close();
150ExpandedSubBlockEnd.gif            }
    
151InBlock.gif
152ExpandedSubBlockEnd.gif        }

153ExpandedSubBlockEnd.gif    }

154ExpandedBlockEnd.gif}

155 None.gif

Add:
 1 None.gif using  System;
 2 None.gif using  System.Collections;
 3 None.gif using  System.ComponentModel;
 4 None.gif using  System.Data;
 5 None.gif using  System.Drawing;
 6 None.gif using  System.Web;
 7 None.gif using  System.Web.SessionState;
 8 None.gif using  System.Web.UI;
 9 None.gif using  System.Web.UI.WebControls;
10 None.gif using  System.Web.UI.HtmlControls;
11 None.gif
12 None.gif namespace  WebTest
13 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
14ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
15InBlock.gif    /// AddInfo 的摘要说明。
16ExpandedSubBlockEnd.gif    /// </summary>

17InBlock.gif    public class AddInfo : System.Web.UI.Page
18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
19InBlock.gif        protected System.Web.UI.WebControls.Button BtOk;
20InBlock.gif        protected System.Web.UI.WebControls.TextBox TextBox1;
21InBlock.gif    
22InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            // 在此处放置用户代码以初始化页面
25ExpandedSubBlockEnd.gif        }

26InBlock.gif
27ContractedSubBlock.gifExpandedSubBlockStart.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
28InBlock.gif        override protected void OnInit(EventArgs e)
29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
30InBlock.gif            //
31InBlock.gif            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
32InBlock.gif            //
33InBlock.gif            InitializeComponent();
34InBlock.gif            base.OnInit(e);
35ExpandedSubBlockEnd.gif        }

36InBlock.gif        
37ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
38InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
39InBlock.gif        /// 此方法的内容。
40ExpandedSubBlockEnd.gif        /// </summary>

41InBlock.gif        private void InitializeComponent()
42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
43InBlock.gif            this.BtOk.Click += new System.EventHandler(this.BtOk_Click);
44InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
45InBlock.gif
46ExpandedSubBlockEnd.gif        }

47ExpandedSubBlockEnd.gif        #endregion

48InBlock.gif
49InBlock.gif        private void BtOk_Click(object sender, System.EventArgs e)
50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
51InBlock.gif            WebTest.Common.COperator AddObj = new WebTest.Common.COperator();
52InBlock.gif            AddObj.NoReturnFunction(TextBox1.Text, "@StrTValues""TS_AddValuesIntoTb");
53ExpandedSubBlockEnd.gif        }

54InBlock.gif
55ExpandedSubBlockEnd.gif    }

56ExpandedBlockEnd.gif}

57 None.gif

Edit/Delete with DataGrid:
  1 None.gif using  System;
  2 None.gif using  System.Collections;
  3 None.gif using  System.ComponentModel;
  4 None.gif using  System.Data;
  5 None.gif using  System.Drawing;
  6 None.gif using  System.Web;
  7 None.gif using  System.Web.SessionState;
  8 None.gif using  System.Web.UI;
  9 None.gif using  System.Web.UI.WebControls;
 10 None.gif using  System.Web.UI.HtmlControls;
 11 None.gif
 12 None.gif namespace  WebTest
 13 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 14ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 15InBlock.gif    /// _Default 的摘要说明。
 16ExpandedSubBlockEnd.gif    /// </summary>

 17InBlock.gif    public class _Default : System.Web.UI.Page
 18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 19InBlock.gif        protected System.Web.UI.WebControls.DataGrid DataGrid1;
 20InBlock.gif    
 21InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
 22ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 23InBlock.gif            // 在此处放置用户代码以初始化页面
 24InBlock.gif            if (!IsPostBack)
 25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 26InBlock.gif                MyDataBind();
 27ExpandedSubBlockEnd.gif            }

 28ExpandedSubBlockEnd.gif        }

 29InBlock.gif
 30ContractedSubBlock.gifExpandedSubBlockStart.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
 31InBlock.gif        override protected void OnInit(EventArgs e)
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 33InBlock.gif            //
 34InBlock.gif            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
 35InBlock.gif            //
 36InBlock.gif            InitializeComponent();
 37InBlock.gif            base.OnInit(e);
 38ExpandedSubBlockEnd.gif        }

 39InBlock.gif        
 40ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 41InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 42InBlock.gif        /// 此方法的内容。
 43ExpandedSubBlockEnd.gif        /// </summary>

 44InBlock.gif        private void InitializeComponent()
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
 46InBlock.gif            this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
 47InBlock.gif            this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
 48InBlock.gif            this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
 49InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
 50InBlock.gif
 51ExpandedSubBlockEnd.gif        }

 52ExpandedSubBlockEnd.gif        #endregion

 53InBlock.gif
 54InBlock.gif        private void MyDataBind()
 55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 56InBlock.gif            WebTest.Common.COperator ShowObj = new WebTest.Common.COperator();
 57InBlock.gif            DataGrid1.DataSource = ShowObj.HaveParameter("SearchAllInfoFromTb");
 58InBlock.gif            DataGrid1.DataBind();
 59ExpandedSubBlockEnd.gif        }

 60InBlock.gif
 61InBlock.gif        public void BtEdit(object sender, System.Web.UI.WebControls.CommandEventArgs e)
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 63InBlock.gif            string Str = e.CommandArgument.ToString();
 64InBlock.gif            // Response.Write("<script>alert('xx')</script>");
 65InBlock.gif            Response.Write(Str);
 66ExpandedSubBlockEnd.gif        }

 67InBlock.gif
 68InBlock.gif        public void BtDelete(object sender, System.Web.UI.WebControls.CommandEventArgs e)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 70InBlock.gif            WebTest.Common.COperator DeleteObj = new WebTest.Common.COperator();
 71InBlock.gif            DeleteObj.NoReturnFunction(e.CommandArgument.ToString(), "@StrID""TS_DeleteInfoFromTb");
 72InBlock.gif            MyDataBind();
 73ExpandedSubBlockEnd.gif        }

 74InBlock.gif
 75InBlock.gif        private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
 76ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 77InBlock.gif            DataGrid1.EditItemIndex = e.Item.ItemIndex;
 78InBlock.gif            MyDataBind();
 79ExpandedSubBlockEnd.gif        }

 80InBlock.gif
 81InBlock.gif        private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 83InBlock.gif            DataGrid1.EditItemIndex = -1;
 84InBlock.gif            MyDataBind();
 85ExpandedSubBlockEnd.gif        }

 86InBlock.gif
 87InBlock.gif        private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 89InBlock.gif            TextBox MyTb = (TextBox)e.Item.FindControl("TextBox1");
 90InBlock.gif            Label MyLb = (Label)e.Item.FindControl("LbID");
 91InBlock.gif            // int IntT = (int)DataGrid1.DataKeyField[e.Item.ItemIndex];
 92InBlock.gif            WebTest.Common.COperator UpdateObj = new WebTest.Common.COperator();
 93InBlock.gif            UpdateObj.NoReturnFunction(MyLb.Text, "@StrTID", MyTb.Text, "@StrTValues""TS_UpdateTb");
 94InBlock.gif
 95InBlock.gif            DataGrid1.EditItemIndex = -1;
 96InBlock.gif            MyDataBind();
 97ExpandedSubBlockEnd.gif        }

 98InBlock.gif
 99InBlock.gif        public void DataGrid1_OnPageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
100ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
101InBlock.gif            //设置当前页的索引
102InBlock.gif            DataGrid1.CurrentPageIndex = e.NewPageIndex;
103InBlock.gif            //重新进行数据绑定
104InBlock.gif            MyDataBind();
105ExpandedSubBlockEnd.gif        }

106ExpandedSubBlockEnd.gif    }

107ExpandedBlockEnd.gif}

108 None.gif

html:
 1 ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false" Inherits="WebTest._Default"  %>
 2 None.gif <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
 3 None.gif < HTML >
 4 None.gif     < HEAD >
 5 None.gif         < title > Default </ title >
 6 None.gif         < meta  content ="Microsoft Visual Studio .NET 7.1"  name ="GENERATOR" >
 7 None.gif         < meta  content ="C#"  name ="CODE_LANGUAGE" >
 8 None.gif         < meta  content ="JavaScript"  name ="vs_defaultClientScript" >
 9 None.gif         < meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
10 None.gif     </ HEAD >
11 None.gif     < body  MS_POSITIONING ="GridLayout" >
12 None.gif         < form  id ="Form1"  method ="post"  runat ="server" >
13 None.gif             < FONT  face ="宋体" >
14 None.gif                 < asp:datagrid  id ="DataGrid1"  style ="Z-INDEX: 101; LEFT: 256px; POSITION: absolute; TOP: 88px"
15 None.gif                    runat ="server"  OnPageIndexChanged ="DataGrid1_OnPageIndexChanged"  AutoGenerateColumns ="False"
16 None.gif                    DataKeyField ="TID"  PageSize ="5"  AllowPaging ="True" >
17 None.gif                     < Columns >
18 None.gif                         < asp:TemplateColumn  HeaderText ="ID" >
19 None.gif                             < ItemTemplate >
20 None.gif                                 < asp:Label  id =LbID  runat ="server"  text ='<%#  DataBinder.Eval(Container.DataItem, "TID") % > '>
21 None.gif                                 </ asp:Label >
22 None.gif                             </ ItemTemplate >
23 None.gif                         </ asp:TemplateColumn >
24 None.gif                         < asp:TemplateColumn  HeaderText ="Values" >
25 None.gif                             < ItemTemplate >
26 None.gif                                 < asp:Label  id =LbValues  runat ="server"  text ='<%#  DataBinder.Eval(Container.DataItem, "TValues") % > '>
27 None.gif                                 </ asp:Label >
28 None.gif                             </ ItemTemplate >
29 None.gif                             < EditItemTemplate >
30 None.gif                                 < asp:TextBox  id =TextBox1  runat ="server"  Text ='<%#  DataBinder.Eval(Container.DataItem, "TValues") % > ' Width="64px">
31 None.gif                                 </ asp:TextBox >
32 None.gif                             </ EditItemTemplate >
33 None.gif                         </ asp:TemplateColumn >
34 None.gif                         < asp:EditCommandColumn  ButtonType ="PushButton"  UpdateText ="更新"  HeaderText ="编辑"  CancelText ="取消"  EditText ="编辑" ></ asp:EditCommandColumn >
35 None.gif                         < asp:TemplateColumn  HeaderText ="删除" >
36 None.gif                             < ItemTemplate >
37 None.gif                                 < asp:Button  id ="BtDelete"  runat ="server"  Text ="删除"  CommandArgument  = '<%#  DataBinder.Eval(Container.DataItem, "TID") % > ' OnCommand = "BtDelete">
38 None.gif                                 </ asp:Button >
39 None.gif                             </ ItemTemplate >
40 None.gif                         </ asp:TemplateColumn >
41 None.gif                     </ Columns >
42 None.gif                     < PagerStyle  HorizontalAlign ="Right"  Mode ="NumericPages" ></ PagerStyle >
43 None.gif                 </ asp:datagrid ></ FONT ></ form >
44 None.gif     </ body >
45 None.gif </ HTML >
46 None.gif
precedure:
1 None.gif    ALTER   PROCEDURE  SearchAllInfoFromTb
2 ExpandedBlockStart.gifContractedBlock.gif /**/ /*
3InBlock.gif    (
4InBlock.gif        @parameter1 datatype = default value,
5InBlock.gif        @parameter2 datatype OUTPUT
6InBlock.gif    )
7ExpandedBlockEnd.gif*/

8 None.gif AS
9 None.gif     select   *   from  Tb
 1 None.gif    ALTER   PROCEDURE  TS_AddValuesIntoTb
 2 None.gif(
 3 None.gif     @StrTValues   nvarchar ( 50 )
 4 None.gif)
 5 None.gif AS
 6 None.gif     insert   into  Tb
 7 None.gif    (TValues)
 8 None.gif     values
 9 None.gif    ( @StrTValues )
10 None.gif
1 None.gif   ALTER   PROCEDURE  TS_DeleteInfoFromTb
2 None.gif(
3 None.gif     @StrID   varchar ( 50 )
4 None.gif)
5 None.gif AS
6 None.gif     delete  Tb
7 None.gif     where  TID  =   @StrID
 1 None.gif    ALTER   PROCEDURE  TS_UpdateTb
 2 None.gif(
 3 None.gif     @StrTID   nvarchar ( 50 ),
 4 None.gif     @StrTValues   nvarchar ( 50 )
 5 None.gif)
 6 None.gif AS
 7 None.gif     update  Tb
 8 None.gif     set  TValues  =   @StrTValues
 9 None.gif     where  TID  =   @StrTID
10 None.gif

转载于:https://www.cnblogs.com/Dtor/archive/2005/11/17/278814.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.6+ based on standard Python type hints. It provides an easy-to-use interface for creating RESTful APIs with minimal boilerplate code. To create a RESTful API for a Schedule, you would typically define the following endpoints: 1. Add: HTTP POST request to add a new schedule item. 2. Delete: HTTP DELETE request to delete an existing schedule item. 3. Select: HTTP GET request to retrieve a list of all schedule items or a single schedule item by ID. 4. Update: HTTP PUT request to update an existing schedule item. Here is an example implementation of these endpoints using FastAPI: ```python from typing import List from fastapi import FastAPI app = FastAPI() class Schedule: def __init__(self, id: int, title: str, description: str, date: str): self.id = id self.title = title self.description = description self.date = date # Mock data schedule_items = [ Schedule(1, "Meeting", "Weekly team meeting", "2022-01-05T10:00:00"), Schedule(2, "Lunch", "Lunch with clients", "2022-01-06T12:30:00"), Schedule(3, "Conference", "Python conference", "2022-01-10T09:00:00"), ] @app.post("/schedule") def add_schedule_item(schedule: Schedule): """ Add a new schedule item. """ schedule_items.append(schedule) return {"message": "Schedule item added successfully."} @app.delete("/schedule/{schedule_id}") def delete_schedule_item(schedule_id: int): """ Delete an existing schedule item by ID. """ for i, schedule in enumerate(schedule_items): if schedule.id == schedule_id: del schedule_items[i] return {"message": "Schedule item deleted successfully."} return {"error": "Schedule item not found."} @app.get("/schedule") def get_all_schedule_items() -> List[Schedule]: """ Retrieve a list of all schedule items. """ return schedule_items @app.get("/schedule/{schedule_id}") def get_schedule_item(schedule_id: int) -> Schedule: """ Retrieve a single schedule item by ID. """ for schedule in schedule_items: if schedule.id == schedule_id: return schedule return {"error": "Schedule item not found."} @app.put("/schedule/{schedule_id}") def update_schedule_item(schedule_id: int, schedule: Schedule): """ Update an existing schedule item by ID. """ for i, s in enumerate(schedule_items): if s.id == schedule_id: schedule_items[i] = schedule return {"message": "Schedule item updated successfully."} return {"error": "Schedule item not found."} ``` With this implementation, you can use HTTP requests to add, delete, select, and update schedule items via the API. For example, to add a new schedule item, you could send a POST request to http://localhost:8000/schedule with a JSON payload containing the schedule data: ```json { "id": 4, "title": "Training", "description": "Training session for new recruits", "date": "2022-01-15T14:00:00" } ``` You could then use GET requests to retrieve the list of all schedule items or a single schedule item by ID, and PUT and DELETE requests to update or delete existing schedule items.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值