C#学习笔记[一]

C#窗口应用如何居中:在form的属性设置里面有一个初始位置的设置(startposion) 设置成centerscreen(屏幕居中即可)

1、读取前N条指定的记录

select top N from 表 where …… order by ….

2、读取最后N条指定的记录(假设总记录有M条)

select top N from 表 where id not in(select top M-N id from talbe)

3、随机读取N条记录

Sql server:   select top n * from 表 order by newid()

Access:        Select top n * FROM 表 orDER BY Rnd(id)

mysql:          Select * From 表 order By rand() Limit n

Request.QueryString[“title”] 适用于Get方法

Request.Form[“title”] 适用于Post方法

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetDB();
        }
    }
    public void GetDB()
    {
        string strsql = "Data Source=localhost;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=hacklzt";
        SqlConnection conn = new SqlConnection(strsql);
        try
        {
            conn.Open();
            string strquery = "select * from NewsInfo";
            SqlCommand cmd = new SqlCommand(strquery, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Response.Write(reader["NewsTitle"]);
            }
        }
        catch (Exception ee)
        {
            Response.Write(ee.ToString());
        }
    }
      protected void Button3_Click(object sender, EventArgs e)
      {
          if (FileUpload1.PostedFile != null)
          {
              Boolean fileok=false;
              string name = FileUpload1.PostedFile.FileName;
              int i = name.LastIndexOf(".");//取最后一个.在名字里面的位置
              string newtext = name.Substring(i,name.Length-i); //取后缀名
              System.DateTime now = System.DateTime.Now;
              string newname = now.DayOfYear.ToString() + FileUpload1.PostedFile.ContentLength.ToString();
              //string newname = now.ToString();
              if (FileUpload1.HasFile)
              {
                  string[] allowedextension = {".gif",".jpeg",".jpg",".rar",".txt"};
                  for (int a=0;a<allowedextension.Length;a++)
                  {
                      if ((newtext == allowedextension[a])&&(FileUpload1.PostedFile.ContentLength<500000))
                      {
                          fileok = true;
                      }
                  }
              }
              if (fileok)
              {
                 try
                 {
                     FileUpload1.PostedFile.SaveAs(Server.MapPath("~\\upload\\" + newname + newtext));
                     Label2.Text = "上传成功";
                 }
                 catch
                 {
                     Label2.Text = "上传失败";
                 } 
              }
              else
              {
                  Label2.Text = "文件格式不对或者文件太大";
              }
          }                  
      }