//Excel导入数据库
private DataSet CreateDataSource()
{
string strCon;
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("/net_admin/exportcj/excel.xls") + "; Extended Properties=Excel 8.0;";
OleDbConnection olecon = new OleDbConnection(strCon);
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
DataSet myds = new DataSet();
myda.Fill(myds);
return myds;
}
/// <summary>
/// 字符串过滤
/// </summary>
/// <param name="strvalue">传来参数</param>
/// <returns>过滤后的字符串</returns>
public string FilterStr(string strvalue)
{
string strfilter = ";|%|*|and|exec|insert|select|delete|update|count|chr|mid|master|truncate|char|declare|script";
string[] strfil = strfilter.Split('|');
foreach ( string str in strfil)
{
strvalue = strvalue.Replace(str,"");
}
return strvalue;
}
Excel导入数据库方法
本文介绍了一种使用C#从Excel文件导入数据到数据库的方法。通过连接Excel文件并读取指定工作表的数据,可以将这些数据填充到DataSet中,便于进一步处理。此外,还提供了一个用于过滤潜在危险字符的字符串过滤函数。
475

被折叠的 条评论
为什么被折叠?



