iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 11
0
自我挑戰組

What a good thing we lose? What a bad thing we knew?系列 第 11

【Day 11】 在Visual Studio 2017 使用LinqToExcel 上傳檔案 (1/2)

大家好,今天跟大家一起學習如何使用LinqToExcel 寫一個批次上傳的功能,因為篇幅有點多,分成兩天來寫XD

Step 1. 下載LinqToExcel
https://ithelp.ithome.com.tw/upload/images/20181025/20112000w97mK1Oe1B.png

Step 2. 輸入
PM> Install-Package LinqToExcel -Version 1.11.0

Step 3. 建立資料庫

https://ithelp.ithome.com.tw/upload/images/20181025/20112000BWU0bmgmmM.png

Step 4. 建立Model

https://ithelp.ithome.com.tw/upload/images/20181025/201120000OvFMt3Kvx.png

https://ithelp.ithome.com.tw/upload/images/20181025/201120007wfzrvgONp.png

public class ExcelModel
{


        public string name { get; set; }

        public string tel { get; set; }
    

}

Step 5. 建立連線字串
5-1 Web.config

  <connectionStrings>
    <add name="connect" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\black\source\repos\ithome2\ithome2\App_Data\Database1.mdf;Integrated Security=True" />
  </connectionStrings>

5-2 Controller

string strConnString = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;

Step 6. Controller 寫一個儲存的資料的Function

6-1. 先Using 會使用的class

    using System.Collections.Generic;
    using System.Configuration;

6-2.再來寫Function

 #region
    public int Save_Excel(string name, string tel)
    {

        using (SqlConnection conn = new SqlConnection(strConnString))
        {
            conn.Open();

            SqlCommand scom = new SqlCommand("", conn);

            scom.CommandText = @"
            	                insert into [telphone]
            			                                    (	
            													name,
                                                                tel

                                                            )
            			                                    values      
            			                                    
            			                                    (	
            		    										@name,
                                                                @tel
            			                                    )                                                                 
                                                             ";

            scom.Parameters.AddWithValue("@name", name);
            scom.Parameters.AddWithValue("@tel", tel);

            int i = scom.ExecuteNonQuery();
            scom.Dispose();
            scom.Clone();
            return i;
        }

    }
    #endregion

參考網址:https://www.nuget.org/packages/LinqToExcel


上一篇
【Day 10】 在Visual Studio 2017 寫一個大樂透產生器
下一篇
【Day 12】 在Visual Studio 2017 使用LinqToExcel 上傳檔案 (2/2)
系列文
What a good thing we lose? What a bad thing we knew?30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言