Oli*_*yre 1 c# performance stored-procedures sqlcommand batch-file
编辑:
我的问题不再是问题了:我重做了我的表演测试并且我做了一个致命的愚蠢错误:我忘记了一个x1000从毫秒获得秒:/对不起那些家伙.
有关信息:
- 我从PC到本地网络上的DataBase服务器每秒进行1900次更新.
- 如果程序与DB相同,则每秒3.200次更新.
- 我的PC上DataBase服务器每秒3.500次更新我不会重新创建并重新打开一个新的SQLConnection.
- 批量文本每秒5.800次更新.对于我的10.000行,如果需要5秒钟,我的程序就可以了.很抱歉让您担心.
实际上,我使用SQL存储的prodedure在我的数据库中创建一行以避免SQL注入.在C#中我有以下方法:
public void InsertUser(string userCode)
{
using (SqlConnection sqlConnection = new SqlConnection(this.connectionString))
{
SqlCommand sqlCommand = new SqlCommand("InsertUser", sqlConnection);
sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
sqlCommand.Parameters.Add(new SqlParameter("@UserCode", userCode));
sqlConnection.Open();
sqlCommand.ExecuteNonQuery();///0.2 seconds !ERROR HERE! 0.2ms here,NOT 0.2sec!!!
}
}
Run Code Online (Sandbox Code Playgroud)
当我有一行或两行插入时,它很棒.但如果我需要创建1.000个用户和10.000个产品以及5000个宠物,那么这不是最好的解决方案:我将在网络传输中浪费大量时间.
我相信,没有签入,我只能使用有限的回调.所以我不想打电话10.000次:
sqlCommand.BeginExecuteNonQuery()
Run Code Online (Sandbox Code Playgroud)
另一种方法是创建批处理文本,但存在SQL注入风险(并且很难看).
是否有一个'SqlCommandList'对象在.Net中管理它?如何在数据库中进行大量写作?对此有什么好处?
Joe*_*orn 10
这应该运行得快一点:
public void InsertUser(IEnumerable<string> userCodes)
{
using (SqlConnection sqlConnection = new SqlConnection(this.connectionString),
SqlCommand sqlCommand = new SqlCommand("InsertUser", sqlConnection))
{
sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter param = sqlCommand.Parameters.Add("@UserCode", SqlDbTypes.VarChar);
sqlConnection.Open();
foreach(string code in userCodes)
{
param.Value = code;
sqlCommand.ExecuteNonQuery();///0.2 seconds
}
}
}
Run Code Online (Sandbox Code Playgroud)
这只会打开一个连接,只创建一个命令,即使你传递了1000个用户.不过,它仍将分别执行每个插入操作.当然,如果userCode不是一个字符串,你需要适当地重新考虑它.您可能还想查看SQL Server的BULK INSERT命令.
| 归档时间: |
|
| 查看次数: |
16915 次 |
| 最近记录: |