一些公用的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Collections;
using System.Web;
using System.Configuration;
using System.IO;
using System.Reflection;
namespace MyCore.control
{
public class Common
{
/// <summary>
/// 通过将抽取到的值与最后一个值进行对调,让永远抽取不到抽取过的值(因为total每次都会减少1,也就是说每次能取到的随机数一直在减少)
/// </summary>
/// <param name="total">数组的长度</param>
/// <param name="selectnum">取的随机数的个数</param>
/// <param name="begin">其实取数</param>
/// <returns></returns>
public int[] GetRandom(int total,int selectnum,int begin)
{
int[] sequence = new int[total];
int[] output = new int[selectnum];
for (int i = 0; i < total; i++)
{
sequence[i] = i+begin;
}
Random random = new Random();
int end = total - 1;
for (int i = 0; i < selectnum; i++)
{
int num = random.Next(0, end + 1);
output[i] = sequence[num];
sequence[num] = sequence[end];
end--;
}
return output;
}
public int IsExistZiduanInTable(string ziduan, string param, string table,string id)
{
int result = 0;
string sql = "select count(*) from " + table + " where IsDeleted = 0 and id <> '"+id+"' and " + ziduan + "= @param";
IDbDataParameter[] parameter = new IDbDataParameter[] { new SqlParameter("@param",param)};
result = MyCore.common.DBHelper.ExecuteScalar(sql, CommandType.Text, parameter);
return result;
}
/// <summary>
/// 判断文件夹是否存在,如果不存在则创建文件夹
/// </summary>
/// <param name="SavePath"></param>
private void ToCreateFile(string SavePath)
{
if (!Directory.Exists(SavePath))
{
Directory.CreateDirectory(SavePath);
}
}
/// <summary>
/// 判断文件是否不在指定的类型中
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private bool IsOutOfFileType(string fileName)
{
bool flag = false;
string fileType = ConfigurationManager.AppSettings["FileType"];
string[] typeList = fileType.Split(';');
foreach (string oneType in typeList)
{
if (oneType == System.IO.Path.GetExtension(fileName))
flag = true;
}
return flag;
}
//上传公告附件
public string[] fileLoad()
{
string []result = new string[3];
string message = "";
HttpFileCollection files = HttpContext.Current.Request.Files;
/// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
for (int iFile = 0; iFile < files.Count; iFile++)
{
string maxSize = ConfigurationManager.AppSettings["FileMaxSize"];
///'检查文件扩展名字
bool fileOK = false;
HttpPostedFile postedFile = files[iFile];
int fileSize = postedFile.ContentLength;
string fileType = postedFile.ContentType;
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
fileExtension = System.IO.Path.GetExtension(fileName);
if (postedFile.ContentLength > (Convert.ToDouble(maxSize) * 1024))
{
message += fileName + " 文件大小大于指定大小,上传失败!";
}
else
{
if (fileName != "")
{
fileOK = IsOutOfFileType(fileName);
}
if (fileOK)
{
string saveFileName = System.Guid.NewGuid() + fileExtension;
string fileSaveDir = System.Web.HttpContext.Current.Request.MapPath("~/Files/") + saveFileName;
result[1] = fileSaveDir;
result[2] = saveFileName;
postedFile.SaveAs(fileSaveDir);
}
else
{
message += fileName + " 文件类型错误,上传失败!";
}
}
}
result[0]=message;
return result;
}
/// <summary>
/// 集合装换DataSet
/// </summary>
/// <param name="list">集合</param>
/// <returns></returns>
/// 2008-08-01 22:08 HPDV2806
public DataSet ToDataSet(IList p_List)
{
DataSet result = new DataSet();
DataTable _DataTable = new DataTable();
if (p_List.Count > 0)
{
PropertyInfo[] propertys = p_List[0].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
_DataTable.Columns.Add(pi.Name, pi.PropertyType);
}
for (int i = 0; i < p_List.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(p_List[i], null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
_DataTable.LoadDataRow(array, true);
}
}
result.Tables.Add(_DataTable);
return result;
}
public DataSet ExecleDs(string filenameurl, string table)
{
DataSet ds = new DataSet();
string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + filenameurl + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'";
if (filenameurl.Contains(".xlsx"))//判断文件是否存在 2007版本
{
strConn = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + filenameurl + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
}
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string tableName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0][2].ToString().Trim();
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [" + tableName + "]", conn);
odda.Fill(ds, table);
return ds;
}
public int InsertQuestionDataSet(DataSet ds)
{
int result = 0;
int rowsnum = ds.Tables[0].Rows.Count;
int columnum = ds.Tables[0].Columns.Count;
string sql = "";
if (rowsnum > 0)
{
sql += "update QuestionInfo set IsDeleted = 1;";
}
for (int i = 0; i < rowsnum; i++)
{
string question = FilteSQLStr(ds.Tables[0].Rows[i]["题目"].ToString().Trim().Replace("'", "''"));
string a = FilteSQLStr(ds.Tables[0].Rows[i]["A"].ToString().Trim().Replace("'", "''"));
string b = FilteSQLStr(ds.Tables[0].Rows[i]["B"].ToString().Trim().Replace("'", "''"));
string c = FilteSQLStr(ds.Tables[0].Rows[i]["C"].ToString().Trim().Replace("'", "''"));
string d = FilteSQLStr(ds.Tables[0].Rows[i]["D"].ToString().Trim().Replace("'", "''"));
string answer = FilteSQLStr(ds.Tables[0].Rows[i]["答案"].ToString().Trim().Replace("'", "''"));
string questionType = ds.Tables[0].Rows[i]["题型"].ToString().Trim().Replace("'", "''");
string courseID = ds.Tables[0].Rows[i]["课程ID"].ToString().Trim().Replace("'", "''");
string sqlStr = "insert into QuestionInfo(Question,A,B,C,D,Answer,QuestionType,CourseID,IsDeleted) values('" + question +
"','" + a + "','" + b + "','" + c + "','" + d + "','" + answer + "','" + questionType + "','" + courseID + "',0);";
sql += sqlStr;
}
IDbDataParameter[] parameter = new IDbDataParameter[] { };
result = MyCore.common.DBHelper.ExecuteNonQuery(sql, CommandType.Text, parameter);
return result;
}
public int InsertStudentDataSet(DataSet ds)
{
int result = 0;
int rowsnum = ds.Tables[0].Rows.Count;
int columnum = ds.Tables[0].Columns.Count;
string sql = "";
if (rowsnum > 0)
{
sql += "update StuInfo set IsDeleted = 1;";
}
for (int i = 0; i < rowsnum; i++)
{
string StuClass = ds.Tables[0].Rows[i]["班级"].ToString().Trim();
string StuNo = ds.Tables[0].Rows[i]["学号"].ToString().Trim();
string StuName = ds.Tables[0].Rows[i]["姓名"].ToString().Trim();
string Sex = ds.Tables[0].Rows[i]["性别"].ToString().Trim();
string pwd = ConfigurationManager.AppSettings["setPwd"];
string sqlStr = "insert into StuInfo(StuClass,StuNo,StuName,Sex,StuPass,IsDeleted) values('" + StuClass + "','" + StuNo +
"','" + StuName + "','" + Sex + "','" + pwd + "',0);";
sql += sqlStr;
}
IDbDataParameter[] parameter = new IDbDataParameter[] { };
result = MyCore.common.DBHelper.ExecuteNonQuery(sql, CommandType.Text, parameter);
return result;
}
/// <summary>
/// 过滤不安全的字符串
/// </summary>
/// <param name="Str"></param>
/// <returns></returns>
public static string FilteSQLStr(string Str)
{
Str = Str.Replace("&", "&");
Str = Str.Replace("<", "<");
Str = Str.Replace(">", ">");
return Str;
}
/// <summary>
/// 还原过滤字段
/// </summary>
/// <param name="Str"></param>
/// <returns></returns>
public static string BackFilteSQLStr(string Str)
{
Str = Str.Replace("&", "&");
Str = Str.Replace("<", "<");
Str = Str.Replace(">", ">");
return Str;
}
}
}
作者:wangqc
出处:http://www.cnblogs.com/wangqc/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-wangqc。