个人的web开发心得(四)----------非常适合入门新手,都是常识

31.SQL关于日期的操作.
Dateadd(wk,datediff(wk,0,getdate()),-1)                             
Dateadd(wk,datediff(wk,0,getdate()),6)

Dateadd(mm,datediff(mm,0,getdate()),0)
Dateadd(ms,-3,dateadd(mm,datediff(m,0,getdate())+1,0))

Dateadd(yy,datediff(yy,0,getdate()),0)
Dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0))

 

32.关于模态窗口(showModalDialog)的专题讨论!   详细见模态窗口压缩包


  1.模态窗口的打开  
  2.模态窗口的关闭  
  3.模态窗口的传递参数。  
  4.其他。。。。  
   
   
  1.window.showModalDialog("DialogPage.aspx","newwin","dialogHeight:   200px;   dialogWidth:   150px;   dialogTop:   458px;   dialogLeft:   166px;   edge:   Raised;   center:   Yes;   help:   Yes;   resizable:   Yes;   status:   Yes;");  
   
  2.window.close();  
   
  3.传值  
  ParentPage.aspx:  
  window.showModalDialog("DialogPage.aspx?para1=aaa&para2=bbb");  
   
  DialogPage.aspx:  
  string   str1=Request.QueryString["para1"].toString();  
  string   str2=Request.QueryString["para2"].toString();  
   
  返回值  
  DialogPage.aspx:  
  window.returnValue="aaa";  
   
  ParentPage.aspx:  
  <script language="jscript">
   function DeptReturn()
   {
    var result = window.showModalDialog('DialogPage.aspx','','width=700,height=400,top=150,left=200,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
    if (result)
    {
        document.aspnetForm.ctl00_contentplaceHolder1_TextBox3.value=result;    //在master页面下控件的名是这样,非master的页面 var tb=document.getElementById ("文本框控件名txtAssessid"); tb.value = result;
    }
   }
  </script>
 
  ParentPage.aspx.cs
  ButtonDept.Attributes["onclick"] = "return DeptReturn();";
   
  4.  
  aspx页面在showmodeldialog情况下为什么一提交就重新打开一个页面?  
  showmodaldialog打开的页面中在<head></head>之间加入一行:<base   target="_self">

 

33.gridview 表中表  数据绑定.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) //在绑定以后执行
    {
        if (e.Row.RowType == DataControlRowType.DataRow)                                //是数据行的话
        {
            string _sql = "select * from Year_ComPlanAllotStandard where PlanId='" + e.Row.Cells[0].Text + "'";// 找到 是绑定 哪一行的记录
            GridView TempGridView = new GridView();                                     //创建一个gridview
            TempGridView = (GridView)e.Row.FindControl("GridViewStandard");  //将模板列中的gridview绑定到新建的girdview上
            DataSet ds = DBC.ExecuDataSet(_sql);     //绑定数据.
            TempGridView.DataSource = ds;
            TempGridView.DataBind();
        }

    }


34.对 gridview 的记录进行删除和更新

    #region 绑定删除确认到 删除按钮
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowType != DataControlRowType.Footer) && (e.Row.RowType != DataControlRowType.Header) && (e.Row.RowType != DataControlRowType.Pager))
        {
            e.Row.Cells[12].Attributes.Add("onclick", "return confirm('您确认要删除这条指标吗?')");
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string _sql = "delete Year_complanallot where id='" + GridView1.Rows[e.RowIndex].Cells[0].Text + "'";
        bool _bool = DBC.Excusql(_sql);

        BuildGridView();

    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            string id = GridView1.Rows[index].Cells[0].Text;
            Response.Redirect("Year_ComPlanAllotListAddEdit.aspx?edittype=1&id=" + id.Trim());

        }
    }
    #endregion


35. 创建事务 执行  并catch{回滚}

            SqlConnection con = new SqlConnection(DBC.strconntstring);
            SqlTransaction transaction;
            con.Open();
            SqlCommand command = con.CreateCommand();

            transaction = con.BeginTransaction();
            command.Connection = con;
            command.Transaction = transaction;

            try
            {
                _sql = "update year_complanallot set Dept='" + TextBox1.Text + "',SortID='" + TextBox2.Text + "',PlanID='" + TextBox6.Text + "',Content='" + TextBox3.Text + "',PlanName='" + TextBox4.Text + "',forecaseTime='" + TextBox5.Text + "' where id='" + id + "'";
                command.CommandText = _sql;
                command.ExecuteNonQuery();
                _sql = "update year_complanallotStandard set planid='" + id + "',standard='" + TextBox7.Text + "',Grade='" + TextBox8.Text + "' where id='" + Label1.Text + "'";
                command.CommandText = _sql;
                command.ExecuteNonQuery();
                _sql = "update year_complanallotStandard set planid='" + id + "',standard='" + TextBox9.Text + "',Grade='" + TextBox10.Text + "' where id='" + Label2.Text + "'";
                command.CommandText = _sql;
                command.ExecuteNonQuery();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
            }
            finally
            {
                con.Close();
            }


36. 获得 插入数据记录的id
string _sql="insert tablename(field....) values(value.....) select @@IDENTITY AS 'id'";


37.绑定日期 年

    /// <summary>
    /// 绑定时间日期到 list
    /// </summary>
    protected void BuildDateTime()
    {

        string sql = "select getdate() ";
        DateTime dt = Convert.ToDateTime(DBC.ExecuScalar(sql));


        year_DropDownList.Items.Add(Convert.ToString(dt.Year - 2));
        year_DropDownList.Items.Add(Convert.ToString(dt.Year - 1));
        year_DropDownList.Items.Add(Convert.ToString(dt.Year));
        year_DropDownList.Items.Add(Convert.ToString(dt.Year + 1));
        year_DropDownList.Items.Add(Convert.ToString(dt.Year + 2));


        #region 选择dropdownlist 为 当前日期

        year_DropDownList.SelectedValue = dt.Year.ToString();

        #endregion
    }

38.根据记录内容 自动选取dropdownlist选项

DropDownList2.Items.FindByValue(dr["planID"].ToString()).Selected = true;


39.传递参数 到后面的页

            Context.Items["zbbm"] = ((Label)e.Item.FindControl("zbbm")).Text.ToString();
            Server.Transfer("year_kpikh.aspx");           
  等同于
              Response.Redirect("year_complandetail.aspx&planid=1");

40.控制显示的数为两位 浮点型

LabelValue.Text = string.Format("{0:f2}", DKpiValue);

 

  http://www.cnblogs.com/tcdwj/archive/2006/09/22/511798.html

你可能感兴趣的:(sql,String,web开发,object,textbox,dataset)