在C#中非常频繁地向数据库插入数据
Insert data to DB very frequently in C#
有什么方法可以save/insert 数据频繁地更新到数据库吗?我有一个列表,其中包含 100 个学生记录的详细信息,如姓名、年龄、class 等。实际上这些数据是从另一个网站获取的,我想将这些记录保存到 sql 服务器 2012。SQLBulkCopy 不是适合这个问题。
我正在使用如下所示的数据库查询:
using (SqlConnection conn = new SqlConnection(DBHelper.DBConnection))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "insert into tableA.....";
cmd.ExecuteNonQuery();
}
conn.Close();
}
目前我正在做 - 我从列表中获取第一个学生记录 [foreach(Student stud in list)] 并使用上面的查询来保存每个学生。
如果我们经常使用sql连接打开和关闭,会不会有什么问题???
有什么办法可以很频繁的把数据存入数据库吗??
请帮忙。
如果您将它们放在一个列表中,则可以在不关闭连接的情况下将它们全部插入一个循环中。打开和关闭与数据库的连接不会导致任何问题,只要关闭的连接在您完成处理后得到妥善处理即可。也就是说,您可以使用 Parallel.ForEach: https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel-foreach-loop or https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel.foreach?view=netcore-3.1 调查项目的并行性。使用它需要引用 System.Threading.Tasks。
编辑:关于如何使用 Parallel.ForEach:
的一些示例代码
using System;
using System.Threading;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.Concurrent;
namespace WindowsFormsApp12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// define the concurrent collection we'll use to leverage parallelism
private ConcurrentBag<Student> allStudents = new ConcurrentBag<Student>();
private void button1_Click(object sender, EventArgs e)
{
foreach(Student s in GetStudents())
{
// populate the concurrent collection with the student object
// depending on how your code works, you may be able to modify the method that gets the data
// from the web services to return this kind of collection and avoid this step
allStudents.Add(s);
}
new Thread(() =>
{
Task.Run(() =>
{
// MaxDegreeOfParallelism can be adjusted to suit your needs. We address the current Student
// object on the thread with the object s
Parallel.ForEach(allStudents, new ParallelOptions { MaxDegreeOfParallelism = 5 }, (s) =>
{
using (SqlConnection con = GetConnection())
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = "INSERT INTO tblStudents ...";
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
});
// you may want to clear the allStudents collection here if your code runs through this
// operation several times in a day
}).Wait();
}).Start();
}
private SqlConnection GetConnection()
{
// return a SqlConnection object wiht an appropriate connection string
return new SqlConnection();
}
private List<Student> GetStudents()
{
// do something to populate the list
return new List<Student>();
}
}
public class Student
{
// class definition for the student object type
public string Name { get; set; }
public int Age { get; set; }
public string Class { get; set; }
}
}
有什么方法可以save/insert 数据频繁地更新到数据库吗?我有一个列表,其中包含 100 个学生记录的详细信息,如姓名、年龄、class 等。实际上这些数据是从另一个网站获取的,我想将这些记录保存到 sql 服务器 2012。SQLBulkCopy 不是适合这个问题。
我正在使用如下所示的数据库查询:
using (SqlConnection conn = new SqlConnection(DBHelper.DBConnection))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "insert into tableA.....";
cmd.ExecuteNonQuery();
}
conn.Close();
}
目前我正在做 - 我从列表中获取第一个学生记录 [foreach(Student stud in list)] 并使用上面的查询来保存每个学生。
如果我们经常使用sql连接打开和关闭,会不会有什么问题???
有什么办法可以很频繁的把数据存入数据库吗??
请帮忙。
如果您将它们放在一个列表中,则可以在不关闭连接的情况下将它们全部插入一个循环中。打开和关闭与数据库的连接不会导致任何问题,只要关闭的连接在您完成处理后得到妥善处理即可。也就是说,您可以使用 Parallel.ForEach: https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel-foreach-loop or https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel.foreach?view=netcore-3.1 调查项目的并行性。使用它需要引用 System.Threading.Tasks。
编辑:关于如何使用 Parallel.ForEach:
的一些示例代码using System;
using System.Threading;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.Concurrent;
namespace WindowsFormsApp12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// define the concurrent collection we'll use to leverage parallelism
private ConcurrentBag<Student> allStudents = new ConcurrentBag<Student>();
private void button1_Click(object sender, EventArgs e)
{
foreach(Student s in GetStudents())
{
// populate the concurrent collection with the student object
// depending on how your code works, you may be able to modify the method that gets the data
// from the web services to return this kind of collection and avoid this step
allStudents.Add(s);
}
new Thread(() =>
{
Task.Run(() =>
{
// MaxDegreeOfParallelism can be adjusted to suit your needs. We address the current Student
// object on the thread with the object s
Parallel.ForEach(allStudents, new ParallelOptions { MaxDegreeOfParallelism = 5 }, (s) =>
{
using (SqlConnection con = GetConnection())
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = "INSERT INTO tblStudents ...";
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
});
// you may want to clear the allStudents collection here if your code runs through this
// operation several times in a day
}).Wait();
}).Start();
}
private SqlConnection GetConnection()
{
// return a SqlConnection object wiht an appropriate connection string
return new SqlConnection();
}
private List<Student> GetStudents()
{
// do something to populate the list
return new List<Student>();
}
}
public class Student
{
// class definition for the student object type
public string Name { get; set; }
public int Age { get; set; }
public string Class { get; set; }
}
}