zoukankan      html  css  js  c++  java
  • ExcelHelp (Excel帮助类)

      public class ExcelHelp
        {
            public static string fileName = @"本地路径 ";
            private static OleDbConnection connection;
            public static OleDbConnection Connection
            {
                get
                {
                    string connectionString = "";
                    string fileType = System.IO.Path.GetExtension(fileName);
                    if (string.IsNullOrEmpty(fileType)) return null;
                    if (fileType == ".xls")
                    {
                        connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";" + ";Extended Properties="Excel 8.0;HDR=YES;IMEX=2"";
                    }
                    else
                    {
                        connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + ";Extended Properties="Excel 12.0;HDR=YES;IMEX=2"";
                    }
                    if (connection == null)
                    {
                        connection = new OleDbConnection(connectionString);
                        connection.Open();
                    }
                    else if (connection.State == System.Data.ConnectionState.Closed)
                    {
                        connection.Open();
                    }
                    else if (connection.State == System.Data.ConnectionState.Broken)
                    {
                        connection.Close();
                        connection.Open();
                    }
                    return connection;
                }
            }
            /// <summary>
            /// 执行无参数的SQL语句
            /// </summary>
            /// <param name="sql">SQL语句</param>
            /// <returns>返回受SQL语句影响的行数</returns>
            public static int ExecuteCommand(string sql)
            {
                OleDbCommand cmd = new OleDbCommand(sql, Connection);
                int result = cmd.ExecuteNonQuery();
                connection.Close();
                return result;
            }
            /// <summary>
            /// 执行有参数的SQL语句
            /// </summary>
            /// <param name="sql">SQL语句</param>
            /// <param name="values">参数集合</param>
            /// <returns>返回受SQL语句影响的行数</returns>
            public static int ExecuteCommand(string sql, params OleDbParameter[] values)
            {
                OleDbCommand cmd = new OleDbCommand(sql, Connection);
                cmd.Parameters.AddRange(values);
                int result = cmd.ExecuteNonQuery();
                connection.Close();
                return result;
            }
            /// <summary>
            /// 返回单个值无参数的SQL语句
            /// </summary>
            /// <param name="sql">SQL语句</param>
            /// <returns>返回受SQL语句查询的行数</returns>
            public static int GetScalar(string sql)
            {
                OleDbCommand cmd = new OleDbCommand(sql, Connection);
                int result = Convert.ToInt32(cmd.ExecuteScalar());
                connection.Close();
                return result;
            }
            /// <summary>
            /// 返回单个值有参数的SQL语句
            /// </summary>
            /// <param name="sql">SQL语句</param>
            /// <param name="parameters">参数集合</param>
            /// <returns>返回受SQL语句查询的行数</returns>
            public static int GetScalar(string sql, params OleDbParameter[] parameters)
            {
                OleDbCommand cmd = new OleDbCommand(sql, Connection);
                cmd.Parameters.AddRange(parameters);
                int result = Convert.ToInt32(cmd.ExecuteScalar());
                connection.Close();
                return result;
            }
            /// <summary>
            /// 执行查询无参数SQL语句
            /// </summary>
            /// <param name="sql">SQL语句</param>
            /// <returns>返回数据集</returns>
            public static DataSet GetReader(string sql)
            {
                OleDbDataAdapter da = new OleDbDataAdapter(sql, Connection);
                DataSet ds = new DataSet();
                da.Fill(ds, "UserInfo");
                connection.Close();
                return ds;
            }
            /// <summary>
            /// 执行查询有参数SQL语句
            /// </summary>
            /// <param name="sql">SQL语句</param>
            /// <param name="parameters">参数集合</param>
            /// <returns>返回数据集</returns>
            public static DataSet GetReader(string sql, params OleDbParameter[] parameters)
            {
                OleDbDataAdapter da = new OleDbDataAdapter(sql, Connection);
                da.SelectCommand.Parameters.AddRange(parameters);
                DataSet ds = new DataSet();
                da.Fill(ds);
                connection.Close();
                return ds;
            }      
           
        }
  • 相关阅读:
    洛谷[P1002]过河卒
    ACM-Teleportation
    ACM-Team Tic Tac Toe
    Data_Structure04-树
    Data_Structure03-栈和队列
    Data_Structure02-线性表
    Data_Structure01-绪论
    C语言第二次实验报告
    C语言第一次实验报告
    mysql
  • 原文地址:https://www.cnblogs.com/nxj1997/p/13224712.html
Copyright © 2011-2022 走看看