Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
QuestionAutomapper exception when converting object with inheriting members Pin
impeham16-Mar-14 5:19
impeham16-Mar-14 5:19 
QuestionHow to access Advanced Class with API Pin
Member 1063699816-Mar-14 4:25
Member 1063699816-Mar-14 4:25 
AnswerRe: How to access Advanced Class with API Pin
Dave Kreskowiak16-Mar-14 7:31
mveDave Kreskowiak16-Mar-14 7:31 
GeneralRe: How to access Advanced Class with API Pin
Member 1063699816-Mar-14 9:50
Member 1063699816-Mar-14 9:50 
GeneralRe: How to access Advanced Class with API Pin
Dave Kreskowiak16-Mar-14 14:16
mveDave Kreskowiak16-Mar-14 14:16 
GeneralRe: How to access Advanced Class with API Pin
Member 1063699816-Mar-14 15:56
Member 1063699816-Mar-14 15:56 
QuestionHow create table in SQL Server using C# and data from Excel. Pin
Sick Series15-Mar-14 4:32
Sick Series15-Mar-14 4:32 
AnswerRe: How create table in SQL Server using C# and data from Excel. Pin
OriginalGriff15-Mar-14 4:48
mveOriginalGriff15-Mar-14 4:48 
It's not something I've done, but...
Loading Excel data to a DataTable is pretty easy:
C#
/// <summary>
/// Load a sheet from an Excel file
/// </summary>
/// <param name="path">Path to excel file</param>
/// <param name="columns">Comma separated list of columns to load. If not specified, loads all</param>
/// <param name="sheet">Sheet name to load. If not specified, loads "Sheet1$"</param>
/// <param name="verify">If true, checks that the file and sheet exist, and throws exceptions if not. Defaults to true</param>
/// <returns>Table of data retrieved.</returns>
public static System.Data.DataTable LoadExcel(string path, string columns = "*", string sheet = "Sheet1$", bool verify = true)
    {
    if (verify)
        {
        if (!File.Exists(path)) throw new IOException("The input file does not exist: " + path);
        if (!GetExcelSheetNames(path).Contains(sheet)) throw new ArgumentException("The requested sheet does not exist: " + sheet);
        }
    if (string.IsNullOrWhiteSpace(columns)) columns = "*";

    System.Data.DataTable dt = new System.Data.DataTable();
    using (OleDbConnection con = new OleDbConnection(GetExcelConnectionString(path)))
        {
        con.Open();
        string cmdStr = string.Format("SELECT {0} FROM [{1}]", columns, sheet);
        using (OleDbCommand cmd = new OleDbCommand(cmdStr, con))
            {
            using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
                {
                da.Fill(dt);
                }
            }
        }
    return dt;
    }
/// <summary>
/// Returns a list of all sheet names in an Excel File.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static List<string> GetExcelSheetNames(string path)
    {
    List<string> sheetNames = new List<string>();
    using (OleDbConnection con = new OleDbConnection(GetExcelConnectionString(path)))
        {
        con.Open();
        using (DataTable sheets = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null))
            {
            foreach (DataRow sheet in sheets.Rows)
                {
                if (sheet["TABLE_NAME"].ToString().Contains("$"))
                    {
                    // Filtered to just sheets - they all end in '$'
                    sheetNames.Add(sheet["TABLE_NAME"].ToString());
                    }
                }
            }
        }
    return sheetNames;
    }
/// <summary>
/// Returns a connection string for an Excel file
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static string GetExcelConnectionString(string path)
    {
    return string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0", path);
    }

And then you need to do is create the DB table from the DataTable. And that is already done as well...if not by me: http://darrylagostinelli.com/2011/06/27/create-a-sql-table-from-a-datatable-in-c-net/[^]
Finally, bulk copy the data over:
C#
/// <summary>
/// Load data into SQL from a DataTable
/// </summary>
/// <param name="strConnect"></param>
/// <param name="tableName"></param>
/// <param name="table"></param>
public static void LoadSql(string strConnect, string tableName, DataTable table)
    {
    using (SqlConnection con = new SqlConnection(strConnect))
        {
        try
            {
            con.Open();
            using (SqlBulkCopy bulk = new SqlBulkCopy(con))
                {
                bulk.DestinationTableName = tableName;
                bulk.WriteToServer(table);
                }
            }
        finally
            {
            con.Close();
            }
        }
    }

Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

AnswerRe: How create table in SQL Server using C# and data from Excel. Pin
Maciej Los15-Mar-14 6:43
mveMaciej Los15-Mar-14 6:43 
GeneralRe: How create table in SQL Server using C# and data from Excel. Pin
OriginalGriff16-Mar-14 0:05
mveOriginalGriff16-Mar-14 0:05 
GeneralW:Re: How create table in SQL Server using C# and data from Excel. Pin
Maciej Los16-Mar-14 0:14
mveMaciej Los16-Mar-14 0:14 
GeneralRe: How create table in SQL Server using C# and data from Excel. Pin
jschell17-Mar-14 8:34
jschell17-Mar-14 8:34 
GeneralRe: How create table in SQL Server using C# and data from Excel. Pin
OriginalGriff17-Mar-14 9:03
mveOriginalGriff17-Mar-14 9:03 
GeneralRe: How create table in SQL Server using C# and data from Excel. Pin
jschell18-Mar-14 8:07
jschell18-Mar-14 8:07 
GeneralRe: How create table in SQL Server using C# and data from Excel. Pin
OriginalGriff18-Mar-14 9:08
mveOriginalGriff18-Mar-14 9:08 
QuestionIEnumerable and disposal Pin
Rob Philpott14-Mar-14 0:31
Rob Philpott14-Mar-14 0:31 
AnswerRe: IEnumerable and disposal Pin
Richard Deeming14-Mar-14 2:06
mveRichard Deeming14-Mar-14 2:06 
GeneralRe: IEnumerable and disposal Pin
Rob Philpott14-Mar-14 2:27
Rob Philpott14-Mar-14 2:27 
GeneralRe: IEnumerable and disposal Pin
Simon_Whale14-Mar-14 2:30
Simon_Whale14-Mar-14 2:30 
GeneralRe: IEnumerable and disposal Pin
Simon_Whale14-Mar-14 2:29
Simon_Whale14-Mar-14 2:29 
AnswerRe: IEnumerable and disposal Pin
jschell14-Mar-14 11:57
jschell14-Mar-14 11:57 
QuestionSharepoint Gridview Pin
DipsMak13-Mar-14 23:21
professionalDipsMak13-Mar-14 23:21 
AnswerRe: Sharepoint Gridview Pin
Richard MacCutchan14-Mar-14 0:21
mveRichard MacCutchan14-Mar-14 0:21 
AnswerRe: Sharepoint Gridview Pin
DipsMak14-Mar-14 0:35
professionalDipsMak14-Mar-14 0:35 
GeneralRe: Sharepoint Gridview Pin
Richard MacCutchan14-Mar-14 1:35
mveRichard MacCutchan14-Mar-14 1:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.