C#我的筆記

前言

記錄一下C#的一些東西,基礎好多仍是不會,仍是推薦微軟的官網文檔,網上的博客寫的都太水了,仍是官網文檔好一點c#

微軟官方文檔異步

異步任務

同步方法的缺點

其實我最想講的就是這個,我舉個例子,有兩個方法,方法1和方法2,我如今想先執行方法1再執行方法2,若是我順序執行的話,那麼必須等待方法1執行完成以後才執行方法2 代碼以下async

static void Main(string[] args)
{
    method1();
    method2();
}

public static void method1()
{
    for (int i = 0; i < 80; i++)
    {
System.Console.WriteLine("method1: "+i);
    }
}
public static void method2() {
    for (int i = 0; i < 20; i++)
    {
System.Console.WriteLine("method2: "+i);
    }
}

執行一下就知道了,必須等待方法1執行完纔會執行方法2,就好比我想燒水作飯,必須先等水燒開了我才能洗菜切菜......這明明是能夠同時作的事情,咱們可使用異步方法解決code

異步方法

這個分爲兩種狀況,I/O和CPU運算,我這裏暫時沒用到I/O因此不寫了,講講CPU運算的文檔

返回Task

static void Main(string[] args)
{
    method1();
    method2();
    System.Console.ReadKey();
}

public static async Task method1()
{
    await Task.Run(() =>
    {
 for (int i = 0; i < 80; i++)
 {
     System.Console.WriteLine("method1: " + i);
 }
    });
}
public static void method2() {
    for (int i = 0; i < 20; i++)
    {
 System.Console.WriteLine("method2: "+i);
    }
}

特色就是async,Task或者Task<T>,await,Task.Run這幾個字符串

返回Task<T>

static void Main(string[] args)
{
    callMethod();
    System.Console.ReadKey();
}

public static async void callMethod()
{
    Task<int> task = method1();
    int count = await task;
    method3(count);
}
public static async Task<int> method1()
{
    int count=0;
    await Task.Run(() =>
    {
 for (int i = 0; i < 80; i++)
 {
     System.Console.WriteLine("method1: " + i);
     count++;
 }
    });
    return count;
}
public static void method2()
{
    for (int i = 0; i < 20; i++)
    {
 System.Console.WriteLine("method2: " + i);
    }
}
public static void method3(int count)
{
    System.Console.WriteLine("Count is "+count);
}

C#讀取CSV,存入數據庫

C#讀取CSV的內容,以DataTable的格式返回get

string path = @"D:\360MoveData\Users\Justin\Desktop\dgkdata\Audio Products~Accessories.csv";


public static DataTable ReadData(string filePath)
        {
            //Encoding encoding = Common.GetType(filePath); //Encoding.ASCII;//
            Encoding encoding = Encoding.ASCII; //Encoding.ASCII;//

            DataTable dt = new DataTable();
            FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            //StreamReader sr = new StreamReader(fs, Encoding.UTF8);
            StreamReader sr = new StreamReader(fs, encoding);
            //string fileContent = sr.ReadToEnd();
            //encoding = sr.CurrentEncoding;
            //記錄每次讀取的一行記錄
            string strLine = "";
            //記錄每行記錄中的各字段內容
            string[] aryLine = null;
            string[] tableHead = null;
            //標示列數
            int columnCount = 0;
            //標示是不是讀取的第一行
            bool IsFirst = true;
            //逐行讀取CSV中的數據
            while ((strLine = sr.ReadLine()) != null)
            {
                if (IsFirst == true)
                {
                    tableHead = strLine.Split(',');
                    IsFirst = false;
                    columnCount = tableHead.Length;
                    //建立列
                    for (int i = 0; i < columnCount; i++)
                    {
                        DataColumn dc = new DataColumn(tableHead[i]);
                        dt.Columns.Add(dc);
                    }
                }
                else
                {
                    //MySplit這個方法看下面的介紹
                        List<string> dataList = MySplit(strLine);
                        aryLine = dataList.ToArray();
                    DataRow dr = dt.NewRow();
                    for (int j = 0; j < columnCount; j++)
                    {
                        dr[j] = aryLine[j];
                    }
                    dt.Rows.Add(dr);
                }
            }
            if (aryLine != null && aryLine.Length > 0)
            {
                dt.DefaultView.Sort = tableHead[0] + " " + "asc";
            }

            sr.Close();
            fs.Close();
            return dt;
        }

而後接受這個DataTable同步

//先獲取全部的列名
            DataTable dt = Read.ReadData(path);
            string[] strColumns = null;
            if (dt.Columns.Count > 0)
            {
                int columnNum = 0;
                columnNum = dt.Columns.Count;
                strColumns = new string[columnNum];
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    strColumns[i] = dt.Columns[i].ColumnName;
                }
            }


//在遍歷開始處理數據
            foreach (DataRow dataRow in dt.Rows)
            {
                foreach (var columsName in strColumns)
                {
                    switch (columsName)
                    {
                        case "Datasheets":
                            break;
                        case "Image":
                            break;
                        處理邏輯......
                    }

                    string aaa = dataRow[columsName].ToString();
                    處理邏輯......
                    Console.WriteLine(aaa);
                }
            }

Split(',')過濾掉雙引號內的逗號

這個也能夠叫作,C#讀取CSV文件逗號問題博客

我讀取的一串字符串是這樣的

"許嵩","蜀,雲泉",1,22,"音樂"

我使用Split(',')以後蜀雲泉就分開了,這顯然不是我要的結果

解決方法可使用正則,可是我不會寫,因此寫一個最基礎的substring

private static List<string> MySplit(string str)
        {
            const char mark = '"';
            const char comma = ',';

            bool startMark = false;
            int startIndex = -1;
            int endIndex = -1;

            List<string> myList = new List<string>();
            for (int i = 0; i < str.Length; i++)
            {
                if (str[0] == comma)
                {
                    myList.Add("");
                }
                if (startMark && str[i] == comma)
                {
                    continue;
                }
                if (str[i] == comma && i > 0)
                {
                    endIndex = i;
                }
                if (str[i] == mark && !startMark)
                {
                    startMark = true;
                }
                else if (str[i] == mark && startMark)
                {
                    startMark = false;
                }
                if (startIndex == -1)
                { startIndex = i; }
                if ((startIndex >= 0 && endIndex > 0) || (endIndex == -1 && i == str.Length - 1))
                {
                    if (endIndex == -1)
                    {
                        endIndex = i + 1;
                    }
                    myList.Add(str.Substring(startIndex, endIndex - startIndex));
                    startIndex = -1;
                    endIndex = -1;
                }
            }
            return myList;
        }

這個strLine就是C#讀取CSV的一行內容

相關文章
相關標籤/搜索