您的位置:首页 > 编程语言

把我写的部分练习程序代码全部奉献了。

2012-12-10 10:24 232 查看
我是一个真正的初学者,拿出自己写的代码奉献给大家,别笑我技术太菜了。

作业管理代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Net; //引入获取IP的命名空间

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btn_Click(object sender, EventArgs e)

{
bool fileOK = false;

//文件的上传路径
string path =Server.MapPath("~/FileUpload1Files/Files/");

//判断上传文件夹是否存在,若不存在,则创建
if (!Directory.Exists(path))
{
//创建文件夹
Directory.CreateDirectory(path);

}

if(FileUpload1.HasFile)

{
//如果选择了文件则执行

//获取上传文件的类型
string fileExtesion = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();

//允许上传的类型
string[] allowExtesions = { ".doc", ".xls", ".rar", ".zip", ".ppt" ,".jpg"};
for (int i = 0; i < allowExtesions.Length; i++)
{
if (fileExtesion == allowExtesions[i])
{
//文件格式正确 允许上传
fileOK = true;
}
}

if (fileOK)
{
try
{
//以时间命名
string newname = System.DateTime.Now.ToString("yyyyMMddHHmmssffff");//声称文件名,防止重复
newname = newname + fileExtesion;
FileUpload1.PostedFile.SaveAs(path + newname);
Session["filename"] = newname;
string ip = Page.Request.UserHostAddress;
string sj = DateTime.Now.ToString();
//保存到数据库中
string s = ConfigurationManager.ConnectionStrings["siteconn"].ConnectionString;            //定义连接字符串
SqlConnection conn = new SqlConnection(s);  //新建数据库连接对象,其中s是上面的连接字符串
conn.Open();    //打开与数据库的连接
string sql = "insert into zuoye(bj,xm,zpmc,sj,ip,filepath) values('" +bj.Text+"','"+xm.Text+"','"+zpmc.Text+"','"+sj+"','"+ip+"','"+ newname + "')"; //定义操作字符串
SqlCommand cmd = new SqlCommand(sql, conn);      //新建数据库操作对象,其sql是操作字符串,conn是连接对象
cmd.ExecuteNonQuery();    //执行操作对象的ExcuteNonQuery方法,该方法返回一个整数,表示受影响的行数
Response.Write("<script language=javascript>{window.alert('记录添加成功!请点击“确定”继续添加');window.location.href='default.aspx';}</script>");    //提示操作成功
conn.Close();   //关闭与数据库的连接

lab_FileUpload1.Text = "上传成功" + Session["filename"]+Convert.ToString(path);

}
catch (Exception)
{

lab_FileUpload1.Text = "上传失败";
//throw ex;
}
}
else
{
lab_FileUpload1.Text = "上传文件类型错误";
}
}
else
{
//尚未选择文件
lab_FileUpload1.Text = "尚未选择任何文件,请选择文件";
return;
}
}
}

删除作业代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;

public partial class del : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
int id = Convert.ToInt32(Request.QueryString["id"]);
conn.ConnectionString = ConfigurationManager.ConnectionStrings["siteConn"].ConnectionString;
//读取数据的sql语句
cmd.CommandText = "SELECT * FROM [zuoye] WHERE Id=" + id.ToString();
cmd.Connection = conn;
conn.Open();//打开数据库连接
reader = cmd.ExecuteReader();//执行数据库操作
if (reader.Read())
{
string wjpath = Convert.ToString(reader["filepath"]);
FileInfo DeleFile = new FileInfo(Server.MapPath("~/FileUpload1Files/Files/") + wjpath);
DeleFile.Delete();
}
reader.Close();
string sql1 = "DELETE FROM [zuoye] WHERE [id] =" + id.ToString(); //定义操作字符串
SqlCommand cmd1 = new SqlCommand(sql1, conn); //新建数据库操作对象,其sql是操作字符串,conn是连接对象
cmd1.ExecuteNonQuery(); //执行操作对象的ExcuteNonQuery方法,该方法返回一个整数,表示受影响的行数
conn.Close(); //关闭与数据库的连接
Response.Redirect("admin.aspx");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐