ASP.NET Excel导入SQL Server数据库是如何实现的呢,前提是ASP.NET Excel导入SQL Server数据库的字段在表里都有,不然会出现错误。
ASP.NET Excel导入SQL Server数据库类文件:
- using System.Data.OleDb;
- using System.Data.SqlClient;
- public class ExcelToSQL
- {
- public SqlConnection sqlconnew SqlConnection("连接字符串"); //创建SQL连接
- public SqlCommand sqlcom; //创建SQL命令对象
- public ExcelToSQL()
- {
- if (sqlcon.State.ToString() == "Open")
- sqlcon.Close();
- }
- public int ImportSql(string excelPath, string tableName) //导入的Excel的路径,数据库里的表名
- {
- if (!TableExist(tableName)) //表名是否存在
- return (int)ImportState.tableNameError;
- DataTable dt = ExcelToDataTable(excelPath);
- if (dt == null)
- {
- return (int)ImportState.excelFormatError;
- }
- ArrayList tableField = GetTableField(tableName); //表格的列名称
- string columnName = "ID,"; //Excel里的列名,增加一个ID列,如果ID自动递增则不需要增加ID列,只需要columnName=“”就可以了。
- for (int i = 0; i < dt.Columns.Count; i++)
- {
- columnName += dt.Columns[i].ColumnName + ",";
- string currentColumn = dt.Columns[i].ToString().ToUpper(); //当前列名
- for (int j = 0; j < tableField.Count; j++)
- {
- if (tableField[j].ToString().ToUpper() == dt.Columns[i].ToString().ToUpper())
- break; //跳出本层和上一层循环,continue是跳出本层循环,如果用continue,会继续执行j++
- //Excel里的字段必须在Sql中都有
- if ((tableField[j].ToString().ToUpper() != dt.Columns[i].ToString().ToUpper()) && j == tableField.Count - 1)
- return (int)ImportState.fieldMatchError;
- }
- }
- int m = columnName.LastIndexOf(',');
- columnName = columnName.Remove(m); //移除最后一个逗号
- sqlcom = new SqlCommand();
- sqlcom.Connection = sqlcon;
- sqlcon.Open();
- sqlcom.CommandType = CommandType.Text;
- for (int h = 0; h < dt.Rows.Count; h++)
- {
- string value = "'" + System.Guid.NewGuid().ToString() + "'" + ","; //如果ID自动递增ID列不需要增加了,那么value的初始值只需要value=“”就可以了。
- for (int k = 0; k < dt.Columns.Count; k++) //根据列名得到值
- {
- value += "'" + dt.Rows[h][k].ToString() + "'" + ",";
- }
- value = value.Remove(0, 1);
- int n = value.LastIndexOf(',');
- value = value.Remove(n); //移除最后一个逗号
- n = value.LastIndexOf("'");
- value = value.Remove(n);
- try
- {
- string sql = "insert into " + tableName + "(" + columnName + ") values('" + value + "')";
- sqlcom.CommandText = sql;
- string sss = sqlcom.ExecuteNonQuery().ToString();
- }
- catch (Exception err)
- {
- string erroe = err.Message;
- return (int)ImportState.dataTypeError;
- }
- }
- sqlcon.Close();
- sqlcom.Dispose();
- return (int)ImportState.right;
- }
- public DataTable ExcelToDataTable(string excelPath) //把Excel里的数据转换为DataTable,并返回DataTable
- {
- string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelPath + ";Extended Properties='Excel 8.0;IMEX=1'";
- System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
- string strCom = "SELECT * FROM [Sheet1$]";
- DataTable dt;
- try
- {
- Conn.Open();
- System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
- DataSet ds = new DataSet();
- myCommand.Fill(ds, "[Sheet1$]");
- Conn.Close();
- dt = ds.Tables[0];
- }
- catch(Exception err)
- {
- return null;
- }
- return dt;
- }
- public bool TableExist(string tableName) //查看数据库里是否有此表名
- {
- sqlcom = new SqlCommand();
- sqlcom.Connection = sqlcon;
- sqlcom.CommandType = CommandType.Text;
- try
- {
- sqlcon.Open();
- string sql = "select name from sysobjects where type='u'";
- sqlcom.CommandText = sql;
- SqlDataReader sqldr = sqlcom.ExecuteReader();
- while (sqldr.Read())
- {
- if (sqldr.GetString(0).ToUpper() == tableName.ToUpper())
- return true;
- }
- }
- catch { return false; }
- finally
- {
- sqlcon.Close();
- }
- return false;
- }
- public ArrayList GetTableField(string tableName) //得到数据库某一个表中的所有字段
- {
- ArrayList al = new ArrayList();
- sqlcom = new SqlCommand();
- sqlcom.Connection = sqlcon;
- sqlcom.CommandType = CommandType.Text;
- try
- {
- sqlcon.Open();
- string sql = "SELECT b.name FROM sysobjects a INNER JOIN syscolumns b ON a.id = b.id WHERE (a.name = '" + tableName + "')";
- sqlcom.CommandText = sql;
- SqlDataReader sqldr = sqlcom.ExecuteReader();
- while (sqldr.Read())
- {
- al.Add(sqldr.GetString(0));
- }
- }
- finally
- {
- sqlcon.Close();
- }
- return al; //返回的是表中的字段
- }
- public enum ImportState
- {
- right = 1, //成功
- tableNameError = 2,//表名不存在
- fieldMatchError = 3,//excel里的字段和数据库表里的字段不匹配
- dataTypeError = 4, //转换数据类型时发生错误
- excelFormatError=5,//Excel格式不能读取
- }
- public void Alert(string str)
- {
- HttpContext.Current.Response.Write("<script language='javascript'>alert('" + str + "');</script>");
- }
- }
ASP.NET Excel导入SQL Server数据库的操作就向你介绍到这里,希望通过实例对你了解ASP.NET Excel导入SQL Server数据库有所帮助。
【编辑推荐】