📄 config.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.IO;
using System.Web.Mail;
using System.Text;
using System.Xml;
using Microsoft.Web.UI.WebControls;
namespace tree
{
/// <summary>
/// config 的摘要说明。
/// </summary>
public class config: Page
{
public SqlConnection myConnection;
public SqlCommand myCommand;
public SqlDataAdapter myAdapter;
public SqlDataAdapter myAdapter1;
public SqlDataReader myReader;
public SqlCommandBuilder myCommandBuilder;
public DataSet ds;
public DataTable dt;
public DataRow dr;
public string filename;
public int filesize;
public config()
{
//
// TODO: 在此处添加构造函数逻辑
//
Open(); // 打开数据库
}
// 1.——————————————————数据库操作函数——————————————————————
/// <summary>
/// 连接数据库
/// </summary>
public void Open()
{
myConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"]);
myConnection.Open();
}
/// <summary>
/// 关闭数据库和清除DateSet对象
/// </summary>
public void Close()
{
if (ds!=null) // 清除DataSet对象
{
ds.Clear();
}
if (myConnection!=null)
{
myConnection.Close(); // 关闭数据库
}
}
/// <summary>
/// 建立DataSet对象,用记录填充或构架(如果必要)DataSet对象,DataSet即是数据在内存的缓存
/// </summary>
/// <param name="str_Sql">打开表Sql语句</param>
public void Fill(string str_Sql)
{
Open();
myAdapter = new SqlDataAdapter(str_Sql,myConnection);
ds = new DataSet();
myAdapter.Fill(ds);
}
/// <summary>
/// 建立DataSet对象,用记录填充或构架(如果必要)DataSet对象,DataSet即是数据在内存的缓存
/// </summary>
/// <param name="tabname">用于表映谢的源表的名称</param>
/// <param name="sql">打开表Sql语句</param>
public void Fill(string tabname,string sql)
{
Open();
myAdapter = new SqlDataAdapter(sql,myConnection);
ds = new DataSet();
myAdapter.Fill(ds,tabname);
}
/// <summary>
/// 给DataSet对象增加映谢表
/// </summary>
/// <param name="str_Sql">Select-SQL语句</param>
public void FillAdd(string str_Sql)
{
myAdapter = new SqlDataAdapter(str_Sql,myConnection);
myAdapter.Fill(ds);
}
/// <summary>
/// 给DataSet对象增加映谢表
/// </summary>
/// <param name="str_Sql">Select-SQL语句</param>
public void FillAdd(string tabname,string str_Sql)
{
myAdapter = new SqlDataAdapter(str_Sql,myConnection);
myAdapter.Fill(ds,tabname);
}
public string GetMaxId(string id,string tablename)
{
string str_Key;
string str_Sql="select top 1 "+id+" from "+tablename+" order by "+id+" desc";
if (GetRowCount(str_Sql)==0)
{
str_Key="1";
}
else
{
GetRowRecord(str_Sql);
str_Key=(int.Parse(dr[id].ToString())+1).ToString(); // 获得数据库表key值
}
return str_Key;
}
/// <summary>
/// 获得包含在DataSet对象的映谢表集合中的index为0的映谢表
/// </summary>
/// <param name="str_Sql">Select-SQL语句</param>
public void GetTable(string str_Sql)
{
Fill(str_Sql);
dt=ds.Tables[0];
}
/// <summary>
/// 获得符合该Sql语句的表记录数
/// </summary>
/// <param name="str_Sql">Select-SQL语句</param>
/// <returns>返回表记录条数</returns>
public int GetRowCount(string str_Sql)
{
Fill(str_Sql);
try
{
int count=ds.Tables[0].Rows.Count;
ds.Clear();
myConnection.Close();
return count;
}
catch
{
ds.Clear();
myConnection.Close();
return 0;
}
}
/// <summary>
/// 通过传Sql语句关键key值获得表中一行的数据
/// </summary>
/// <param name="str_Sql">带关键Key值参数的Select-SQL语句</param>
public void GetRowRecord(string str_Sql)
{
Fill(str_Sql);
dr=ds.Tables[0].Rows[0];
myConnection.Close();
}
/// <summary>
/// 执行Transact-SQL语句,对数据库记录做插入,修改,删除等操作
/// </summary>
/// <param name="str_Sql">Transact-SQL语句</param>
public void ExeSql(string str_Sql)
{
Open();
myCommand = new SqlCommand(str_Sql,myConnection);
myCommand.ExecuteNonQuery();
myCommand.Dispose();
}
/// <summary>
/// 在DataGrid控件中删除数据库记录
/// </summary>
/// <param name="id">数据库表关键字key字段名</param>
/// <param name="tablename">数据库表名</param>
/// <param name="cb_Id">CheckBox控件id值</param>
/// <param name="lbl_Id">Label控件id值</param>
/// <param name="myDataGrid">DataGrid控件,即是数据绑定列表</param>
public void ExeDel(string id,string tablename,string cb_Id,string lbl_Id,DataGrid myDataGrid)
{
for (int i=0;i<myDataGrid.Items.Count;i++)
{
CheckBox myCheckBox;
Label myLabel;
myCheckBox=(CheckBox)myDataGrid.Items[i].FindControl(cb_Id);
myLabel=(Label)myDataGrid.Items[i].FindControl(lbl_Id);
if(myCheckBox.Checked==true)
{
string strDelSql="delete from "+tablename+" where "+id+"="+myLabel.Text;
ExeSql(strDelSql);
}
}
}
/// <summary>
/// 在DataGrid控件中删除数据库记录,连同该条记录图片一起删除
/// </summary>
/// <param name="id">数据库表关键字key字段名</param>
/// <param name="imgs">数据库表中保存图片文件路径字段名</param>
/// <param name="tablename">数据库表名</param>
/// <param name="cb_Id">CheckBox控件id值</param>
/// <param name="lbl_Id">Label控件id值</param>
/// <param name="myDataGrid">DataGrid控件,即是数据绑定列表</param>
public void ExeDel(string id,string imgs,string tablename,string cb_Id,string lbl_Id,DataGrid myDataGrid)
{
for (int i=0;i<myDataGrid.Items.Count;i++)
{
CheckBox myCheckBox;
Label myLabel;
myCheckBox=(CheckBox)myDataGrid.Items[i].FindControl(cb_Id);
myLabel=(Label)myDataGrid.Items[i].FindControl(lbl_Id);
if(myCheckBox.Checked==true)
{
string strDelImgSql="select * from "+tablename+" where "+id+"="+myLabel.Text;
GetRowRecord(strDelImgSql);
if (dr[imgs].ToString()!="")
{
if (GetStrCount(dr[imgs].ToString(),",")==0)
{
if (File.Exists(Server.MapPath(dr[imgs].ToString())))
{
File.Delete(Server.MapPath(dr[imgs].ToString()));
}
}
else
{
string[] img=dr[imgs].ToString().Split(',');
for (int j=0;j<img.Length;j++)
{
if (File.Exists(Server.MapPath(img[j])))
{
File.Delete(Server.MapPath(img[j]));
}
}
}
}
string strDelSql="delete from "+tablename+" where "+id+"="+myLabel.Text;
ExeSql(strDelSql);
}
}
}
/// <summary>
/// 在Repeater控件中删除数据库记录
/// </summary>
/// <param name="id">数据库表关键字key字段名</param>
/// <param name="tablename">数据库表名</param>
/// <param name="cb_Id">CheckBox控件id值</param>
/// <param name="lbl_Id">Label控件id值</param>
/// <param name="myDataGrid">DataGrid控件,即是数据绑定列表</param>
public void ExeDel(string id,string tablename,string cb_Id,string lbl_Id,Repeater myDataGrid)
{
for (int i=0;i<myDataGrid.Items.Count;i++)
{
CheckBox myCheckBox;
Label myLabel;
myCheckBox=(CheckBox)myDataGrid.Items[i].FindControl(cb_Id);
myLabel=(Label)myDataGrid.Items[i].FindControl(lbl_Id);
if(myCheckBox.Checked==true)
{
string strDelSql="delete from "+tablename+" where "+id+"="+myLabel.Text;
ExeSql(strDelSql);
}
}
}
/// <summary>
/// 在Repeater控件中删除数据库记录,连同该条记录图片一起删除
/// </summary>
/// <param name="id">数据库表关键字key字段名</param>
/// <param name="imgs">数据库表中保存图片文件路径字段名</param>
/// <param name="tablename">数据库表名</param>
/// <param name="cb_Id">CheckBox控件id值</param>
/// <param name="lbl_Id">Label控件id值</param>
/// <param name="myDataGrid">DataGrid控件,即是数据绑定列表</param>
public void ExeDel(string id,string imgs,string tablename,string cb_Id,string lbl_Id,Repeater myDataGrid)
{
for (int i=0;i<myDataGrid.Items.Count;i++)
{
CheckBox myCheckBox;
Label myLabel;
myCheckBox=(CheckBox)myDataGrid.Items[i].FindControl(cb_Id);
myLabel=(Label)myDataGrid.Items[i].FindControl(lbl_Id);
if(myCheckBox.Checked==true)
{
string strDelImgSql="select * from "+tablename+" where "+id+"="+myLabel.Text;
GetRowRecord(strDelImgSql);
if (dr[imgs].ToString()!="")
{
if (GetStrCount(dr[imgs].ToString(),",")==0)
{
if (File.Exists(Server.MapPath(dr[imgs].ToString())))
{
File.Delete(Server.MapPath(dr[imgs].ToString()));
}
}
else
{
string[] img=dr[imgs].ToString().Split(',');
for (int j=0;j<img.Length;j++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -