2 Star 33 Fork 4

Rambo / online-music

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
musicChange.aspx.cs 5.04 KB
一键复制 编辑 原始数据 按行查看 历史
Rambo 提交于 2017-01-27 16:28 . 项目初始化
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class musicChange : System.Web.UI.Page
{
//定义一个table容器
public Hashtable typeindex;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
db_music();
}
//初始化并添加数据
typeindex = new Hashtable();
typeindex["流行"] = 0;
typeindex["经典"] = 1;
typeindex["日韩"] = 2;
typeindex["欧美"] = 3;
typeindex["其它"] = 4;
}
//删除按钮绑定事件
protected void dg_DeleteCommand(object source, DataGridCommandEventArgs e)
{
//dg.Attributes["OnClick"] = "return confirm('你确认要删除吗?')";
//初始化一个数据库连接对象
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
//记录当前要删除的歌曲的ID
string id = dg.DataKeys[e.Item.ItemIndex].ToString();
//打开这个连接数据库
conn.Open();
//初始化一个数据库实务
SqlTransaction myTrans = conn.BeginTransaction();
try
{
SqlCommand cmd = new SqlCommand();
cmd.Transaction = myTrans;
cmd.CommandText = "select * from [music] where id=@id";
cmd.Connection = conn;
cmd.Parameters.Add("@id", SqlDbType.NChar).Value = dg.DataKeys[e.Item.ItemIndex].ToString();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string musicname = dr.GetString(1);
string album = dr.GetString(3);
string singer = dr.GetString(2);
dr.Close();
cmd.Parameters.Add("@singer", SqlDbType.NChar).Value = singer;
cmd.Parameters.Add("@album", SqlDbType.NChar).Value = album;
cmd.CommandText = "delete from [music] where id = @id";
cmd.ExecuteNonQuery();
if (!isExisted("singer", "music", conn, cmd))
{
cmd.CommandText = "delete from [singer] where singer=@singer";
cmd.ExecuteNonQuery();
}
if (!isExisted("album", "music", conn, cmd))
{
cmd.CommandText = "delete from [album] where album=@album";
cmd.ExecuteNonQuery();
}
myTrans.Commit();
string path = new DirectoryInfo(Server.MapPath("")).FullName.ToString() + @"\file\" + musicname;
path = path.Replace('\\', '/');
File.Delete(path);
}
db_music();
}
catch (Exception ex)
{
msg.Text = ex.ToString();
myTrans.Rollback();
}
finally
{
conn.Close();
}
}
protected Boolean isExisted(string keyword, string table, SqlConnection con, SqlCommand cmd)
{
cmd.CommandText = "select * from [" + table + "] where " + keyword + "=@" + keyword;
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
Boolean check = dr.Read();
dr.Close();
return check;
}
protected void dg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
dg.CurrentPageIndex = e.NewPageIndex;
db_music();
}
//页面初始化绑定数据函数
public void db_music()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
try
{
msg.Text = "";
string sqlselect = "SELECT * FROM [music]";
SqlCommand cmd = new SqlCommand(sqlselect, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
SqlCommandBuilder sqlcb = new SqlCommandBuilder(da);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
dg.DataSource = dt;
dg.DataBind();
}
else
{
msg.Text = "暂时还没有添加歌曲!";
dg.Visible = false;
}
//弹出是否删除框
for (int i = 0; i < this.dg.Items.Count; i++)
{
this.dg.Items[i].Cells[1].Attributes.Add("onclick", "return confirm('确定删除该记录?')");
}
}
catch (Exception ex)
{
msg.Text = ex.Message.ToString();
}
finally
{
conn.Close();
}
}
public int gettype(string type)
{
if (typeindex[type] != null)
return (int)typeindex[type];
else
return 0;
}
}
ASP
1
https://gitee.com/LanboEx/online-music.git
git@gitee.com:LanboEx/online-music.git
LanboEx
online-music
online-music
master

搜索帮助