zoukankan      html  css  js  c++  java
  • VS2010 sql2008制作网站中的一些代码

    1.连接数据库:

    using system.data;

    using system.data.sqlclint;

    String sqlconn = "Server=.;Database=USER;uid=sa;password=890302";

    SqlConnection myconnection = new SqlConnection(sqlconn);

    myconnection.Open();

    SqlCommand mycommand = new SqlCommand("select * from USERINFO where account='" +string1+ "' and password='"+string2+"'", myconnection);//登陆信息查证

    SqlDataReader myreader;
    myreader = mycommand.ExecuteReader();
    if (myreader.HasRows)    //判断是否存在数据
    {
    while (myreader.Read())
    {s1 = myreader[2].ToString();}}

     myconnection.Close();

             1.1sql插入数据   

    SqlCommand mycommand = new SqlCommand("insert into USERINFO values('" + s1+ "','" + ss2+ "','" + s3 + "')", myconnection);
    mycommand.ExecuteNonQuery();

            1.2sql修改数据

    SqlCommand mycommand1 = new SqlCommand("update USERINFO set password='" + USER.spw + "' where account='" + USER.sac + "'", myconnection);
    mycommand1.ExecuteNonQuery();

    2.页面跳转语句:

       server.transfer(“aa.aspx”);

       System.Diagnostics.Process.Start("www.baidu.com");

       Response.Redirect(“www.baidu.com”,false);

    3.当前时间:

       datetime.now.tostring();

    4.数据绑定:

      gridview创建hyperlink列行可以实现点击条目进行跳转,dataNavigationUrlField为作为传递的参数可设为:ID,dataNavigationFomatString为要连接到的网页,可设置为:aa.aspx?id={0}。DataTextField为在gridview中显示的文本。

      在新网页中获取传递参数的方法为:int ID = Int32.Parse(Request.Params["ID"]);

    5.下载文件:

    using System.IO;

    string filepath = Server.MapPath("~/image/11.jpg"); //获取文件的服务器路径
    string filename = Path.GetFileName(filepath); //通过路径获得文件
    FileInfo file = new FileInfo(filepath); //创建FileInfo类的实例
    Response.Clear();
    Response.ContentType = "application/octet-stream"; //文件的输出类型
    Response.AddHeader("Content-Disposition", "attachment; filename = " + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));//输出文件头
    Response.AddHeader("Content-length", file.Length.ToString());//输出文件的大小
    Response.Flush();
    Response.WriteFile(filepath);//输出文件
    Response.End();

    6.上传文件:

    if (FileUpload1.HasFile)
    {FileUpload1.PostedFile.SaveAs("E:\\" + FileUpload1.FileName);}

    7.刷新当前页面:

    public static void refresh()
    { HttpContext.Current.Response.Redirect(HttpContext.Current.Request.RawUrl); }

     8.关于全局变量:

    C#中可以通过在类中设置静态变量作为全局变量。

    public class USER
    {public static string s1;}

    在其他网页中可用:USER.s1 调用。

    9.C#扩展方法:

    namespace ExtensionMethods
    { public static class MyExtensions

        {    public static int WordCount(this String str)
          {   return str.Split(new char[] { ' ''.''?' }, 

                  StringSplitOptions.RemoveEmptyEntries).Length;
            }
        } 

      
    }

     可使用以下 using 指令将 WordCount 扩展方法放入范围中:

    using ExtensionMethods;

     程序中可如此使用:

    string s = "Hello Extension Methods";
    int i = s.WordCount();

     来自:http://msdn.microsoft.com/zh-cn/library/bb383977.aspx

    10.历史访问量和在线用户数:

    Global.asax文件:

     void Application_Start(object sender, EventArgs e) 
        {
            
    //在应用程序启动时运行的代码
            Application["CurrentGuests"= 0;//初始化为0;
            
    //Server.MapPath获取的是物理路径(绝对路径)
            
    //1. Server.MapPath("./") 当前目录 2.Server.MapPath("../")上一级目录  3.Server.MapPath("~/")网站根目录
            FileStream fileStream = File.Open(Server.MapPath("counts.text"), FileMode.OpenOrCreate);//文件不存在,创建文件
            StreamReader reader = new StreamReader(fileStream);//要读取的完整路径
            Application["AllGuests"= Convert.ToInt32(reader.ReadLine()); //从当前流中读取一行字符并将数据作为字符串返回
            reader.Close();//关闭流
        } 
        
    void Application_End(object sender, EventArgs e) 
        {
       }
        
    void Application_Error(object sender, EventArgs e) 
        { 
    }

        
    void Session_Start(object sender, EventArgs e) //当用户访问网站时,在线用户+1,总访问数+1
        {
            
    //在新会话启动时运行的代码
            Application.Lock();//同步,避免同时写入
            Application["CurrentGuests"= (int)Application["CurrentGuests"+ 1;//总在线用户数
            Application["AllGuests"= (int)Application["AllGuests"+ 1;//访问网站的总用户数
            
            FileStream fileStream 
    = new FileStream(Server.MapPath("counts.txt"), FileMode.OpenOrCreate, FileAccess.ReadWrite);//
            StreamWriter writer = new StreamWriter(fileStream);//实现一个写入流,使其以一种特定的编码向流中写入字符
            writer.WriteLine(Application["AllGuests"].ToString());//把访问网站的总用户数再次写入到文件
            writer.Close();//关闭写入流
            Application.UnLock();//同步结束
        }
        
    void Session_End(object sender, EventArgs e) 
        {
            
    //在会话结束时运行的代码。 
            
    // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            
    // InProc 时,才会引发 Session_End 事件。如果会话模式 
            
    //设置为 StateServer 或 SQLServer,则不会引发该事件。
            Application.Lock();
            Application[
    "CurrentGuests"= (int)Application["CurrentGuests"- 1;//总在线用户数量-1
            Application.UnLock(); 

    }

    在任何界面中可以通过下面代码进行显示:

    Label5.Text = "在线人数:" + Application["user_sessions"].ToString();
    Label6.Text = "总访问量:" + Application["AllGuests"].ToString();

     11.历史访问量和在线用户数:

    在vs.net2010解决方案资源管理器中,右击你的项目--》添加--》新建项--》
    在弹出的新窗体中的左边,已安装的模板中选择"visual C#项",
    然后在中间的列表框中选择"LINQ to SQL Classed" 在本窗体下方的“名称”输入框中输入"LinqDB.dbml"
    再点击"添加"按钮,你就能在"LinqDB.designer.cs"文件中看到LinqDBDataContext 了。

     

  • 相关阅读:
    利用数据库复制技术 实现MSSQL数据同步更新
    育子两篇你会教育自已的小孩吗
    hdu 1046 Gridland (找规律题)
    hdu 1022 Train Problem I (栈的操作,还水了半天)
    hdu 4022 Bombing (强大的map一对多的映射)
    POJ 1702 Eva's Balance (数论,平衡三进制)
    hdu 3951 Coin Game (博弈)
    hdu 1058 Humble Numbers (DP初步)
    hdu 2084 数塔 (DP初步)
    hdu 1056 HangOver (打表水题)
  • 原文地址:https://www.cnblogs.com/veprayer/p/2664055.html
Copyright © 2011-2022 走看看