Dapper批量添加
Public void Add() {
List<PPQuery> lists = GetDataByFile(tempFilePath); private static readonly string sqlconnection = "Data Source=.;Initial Catalog=xxxx;Persist Security Info=True;User ID=sa;Password=123"; using (var connection = new System.Data.SqlClient.SqlConnection(sqlconnection)) { resultData = connection.Execute("Insert into [PPQuery] values (@ID,@Name,@Time,@Company)", lists); } }
public List<PPQuery> GetDataByFile(string path) { var file = File.Open(path, FileMode.Open); List<string> txt = new List<string>(); using (var stream = new StreamReader(file, System.Text.Encoding.GetEncoding("utf-8"))) { while (!stream.EndOfStream) { txt.Add(stream.ReadLine()); } } file.Close(); List<PPQuery> models = new List<PPQuery>(); //循环list foreach (string item in txt) { string[] tempstr = item.Split(','); PPQuery model = new PPQuery(); model.ID = tempstr[0]; model.Name = tempstr[1]; model.Time =Convert.ToDateTime(tempstr[2]); model.Company = tempstr[3]; models.Add(model); } return models; }