I know the basics of SQL and so I have learned to create Insert Queries like the following one:
queryAccount.AppendLine(
string.Format(
"Insert INTO Account(Number_Account, DoID, ClientID) Select "
+ "{0}, "
+ "(Select id From AccountDO Where Number_do = {1}), "
+ "(Select id From Client Where Number_Client = {2})"
+ "Where not exists(Select * From Account Where Number_Account = {0});",
item.Client.NumberAccount,
item.Client.NumberDo,
item.Client.NumberClient));
In that query I add data to a table "Account" that has two FKs ( DoID
and ClientID
), and I also check if that account already exists. Usually, to insert data from a flat file I use a String Builder in order to create multiple insert queries.
This works well in some projects with low requisites, but now I have a bigger challenge in hands. I need to create a web site that imports new data on a daily basis, and so it's important to have the "import module" following the best practices.
What I have done so far:
What I need to do:
And that is why I need your help, how can I make the best use of the technologies available in order to achieve my goals? Is it possible to use the Entity Framework (EF) in order to add a List of Accounts into the DataSet with little effort?.
Your requirements:
If you are using EF to insert data from your C# code, you can consider using parameterized sql queries to make your inserts safer from SQL injection attacks.
Using Data.SqlClient.SqlCommand.Parameters.Add
:
MSDN: SqlCommand.Parameters Property
public void InsertCustomer(Integer customerID, DateTime activityDate) {
String sql = "INSERT INTO Customers (customerID, ActivityDate) VALUES (@customerID, @activityDate);";
Data.SqlClient.SqlCommand cmd = new Data.SqlClient.SqlCommand(sql);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@customerID ", Data.SqlDbType.Int).Value = customerID;
cmd.Parameters.Add("@activityDate ", Data.SqlDbType.DateTime).Value = activityDate;
try {
using (SqlConnection connection = new Data.SqlClient.SqlConnection(YourConnectionString)) {
connection.Open();
cmd.Connection = connection;
cmd.ExecuteNonQuery();
}
} catch (Exception ex) {
throw ex;
}
}
However your insert jobs should run faster if you use SSIS or T-SQL BULK INSERTS.
Here are the resources I found:
Insert and Update Records with an SSIS ETL Package
Bulk Import and Export of Data (SQL Server)
Optimizing Bulk Import Performance
Prerequisites for Minimal Logging in Bulk Import
Minimal logging requires that the target table meets the following conditions:
Whether minimal logging can occur for a table also depends on whether the table is indexed and, if so, whether the table is empty:
Bulk Inserts via TSQL in SQL Server
Assuming you have properly mapped your database (either code-first or database-first), you should have a handful of tables mapped to your context. For example:
public class DataModel : DbContext
{
/* more code ... */
public virtual DbSet<User> Users { get; set; }
/* more code ... */
}
The DbSet
class exposes an AddRange
method you can use for bulk inserts. So, assuming you had a collection of User
objects, you could do this:
public class SomeClass
{
public int InsertUsers(params User[] users)
{
using(var context = new DataModel())
{
context.Users.AddRange(users);
}
}
}
The users will be inserted in one transaction (assuming the underlying datastore supports transactions).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.