public static byte[] streamToByteArray(Stream stream) { if (stream == null) throw new ArgumentNullException(nameof(stream)); if (!stream.CanRead) throw new NotSupportedException("Cannot copy from a write-only stream."); using (MemoryStream ms = new MemoryStream()) { stream.CopyTo(ms); return ms.ToArray(); } }
public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddSingleton<IConfiguration>(Configuration); }
public MyController(IConfiguration configuration) { _configuration = configuration; } private void importdata(IList<string> lines) { string connectionString = _configuration["SQLDB:SQLDBCONN:MySQLConnStr"]; }
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SQLDB:SQLDBCONN:MySQLConnStr"))); }
string connectionString = ConfigurationManager.ConnectionStrings["SQLDBCONN"].ConnectionString;
public string toTbl(IList < string > records) { const string connectionString = @"Data Source=dbsvr;Initial Catalog=dbname;User Id=usr;Password=pwd;"; DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("ID", typeof(string))); dt.Columns.Add(new DataColumn("Student", typeof(string))); dt.Columns.Add(new DataColumn("Grade", typeof(string))); foreach (string item in records.Skip(1)) { dt.Rows.Add(new object[] { item }); } using (var conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlBulkCopy s = new SqlBulkCopy(conn)) { s.DestinationTableName = "Student"; s.BatchSize = dt.Rows.Count; s.BulkCopyTimeout = 0; s.ColumnMappings.Add("ID", "ID"); s.ColumnMappings.Add("Student", "Student"); s.ColumnMappings.Add("Grade", "Grade"); s.WriteToServer(dt); s.Close(); } conn.Close(); } }