如何 "reset" C# SqlCommand 对象以便我可以在循环中重新使用它
How to "reset" C# SqlCommand object so I can re-use it in a loop
我在 foreach 循环中有这段代码,它为每个文件夹调用 sql 函数
foreach (string strCurrentFolder in strLocalSubFolderList)
{
SqlCommand sqlComm1 = new SqlCommand("dbo.fnChkXfer", _sqlConn);
sqlComm1.CommandType = CommandType.StoredProcedure;
sqlComm1.Parameters.Add("@FileXferred",SqlDbType.Bit).Direction = ParameterDirection.ReturnValue;
sqlComm1.Parameters.AddWithValue("@UNCFolderPath", strCurrentFolder);
sqlComm1.Parameters.AddWithValue("@FileType", "Type 1");
...if files not transferred, then transfer them
SqlCommand sqlComm2 = new SqlCommand("dbo.fnChkXfer", _sqlConn);
sqlComm2.CommandType = CommandType.StoredProcedure;
sqlComm2.Parameters.Add("@FileXferred",SqlDbType.Bit).Direction = ParameterDirection.ReturnValue;
sqlComm2.Parameters.AddWithValue("@UNCFolderPath", strCurrentFolder);
sqlComm2.Parameters.AddWithValue("@FileType", "Type 2");
...if files not transferred, then transfer them
SqlCommand sqlComm3 = new SqlCommand("dbo.fnChkXfer", _sqlConn);
sqlComm3.CommandType = CommandType.StoredProcedure;
sqlComm3.Parameters.Add("@FileXferred",SqlDbType.Bit).Direction = ParameterDirection.ReturnValue;
sqlComm3.Parameters.AddWithValue("@UNCFolderPath", strCurrentFolder);
sqlComm3.Parameters.AddWithValue("@FileType", "Type 3");
...if files not transferred, then transfer the
}
它工作正常,但是有很多重复的代码,这可能是不必要的。
是否可以在循环外创建 SqlCommand 对象并 "reset" 仅在循环中创建参数值?我怎样才能重用这个对象? (请非常具体)。
或者我应该继续将这段代码包含在循环中,每次迭代使用不同的数据执行 fn 3 次?每次都几乎完全相同时,继续重新创建 SqlCommand 对象似乎效率低下。
您绝对可以(甚至应该)在循环外创建 SqlCommand
,并随着循环的进行不断更改参数。为此,您需要在添加参数时存储它们,然后在循环中设置它们的值。您还应该在完成命令后关闭命令,最好以某种自动方式关闭。 using
语句是最常见的选择。
using (var cmd = new SqlCommand("dbo.fnChkXfer", _sqlConn)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@FileXferred",SqlDbType.Bit).Direction = ParameterDirection.ReturnValue;
var p2 = cmd.Parameters.AddWithValue("@UNCFolderPath", DbType.Varchar, 32); // << Set the correct size
var typeParam = cmd.Parameters.AddWithValue("@FileType", , DbType.Varchar, 32);
foreach (string strCurrentFolder in strLocalSubFolderList) {
p2.Value = strCurrentFolder;
foreach (var typeVal in new[] {"Type 1", "Type 2", ...}) {
typeParam.Value = typeVal;
... // Set values of the remaining parameters
... // Use your command as needed
}
}
}
// After this point all three commands will be closed automatically
我将您的列表更改为字典以添加文件类型。如果要重用同一个 sqlcommand 对象,则需要在每次循环迭代中执行它。所以我添加了那部分。您可能想尝试一下,也赶上那里。
Dictionary<string,string> strLocalSubFolderDict = new Dictionary<string, string>();
strLocalSubFolderDict.Add( "Type 1", "Directory 1");
strLocalSubFolderDict.Add( "Type 2", "Directory 2");
strLocalSubFolderDict.Add( "Type 3", "Directory 3");
using (SqlCommand sqlComm = new SqlCommand("dbo.fnChkXfer", _sqlConn))
{
sqlComm.CommandType = CommandType.StoredProcedure;
sqlComm.Parameters.Add("@FileXferred", SqlDbType.Bit).Direction = ParameterDirection.ReturnValue;
sqlComm.Parameters.Add("@UNCFolderPath");
sqlComm.Parameters.Add("@FileType");
foreach (var val in strLocalSubFolderDict)
{
sqlComm.Parameters["@UNCFolderPath"].Value = val.Value;
sqlComm.Parameters["@FileType"].Value = val.Key;
sqlComm.ExecuteNonQuery();
// ...if files not transferred, then transfer them
}
}
在此代码中,对象是在循环外创建的,foreach 中唯一的更改是对对象参数的值进行更改。
另一方面,我不太确定您使用 FileXferred return 参数做什么。你在什么地方用过它吗?
更新
这是每个目录都应用了所有文件类型的代码。
List<string> strLocalSubFolderList = new List<string>();
List<string> typesList = new List<string>();
typesList.Add("Type 1");
typesList.Add("Type 2");
typesList.Add("Type 3");
using (SqlCommand sqlComm = new SqlCommand("dbo.fnChkXfer", _sqlConn))
{
sqlComm.CommandType = CommandType.StoredProcedure;
sqlComm.Parameters.Add("@FileXferred", SqlDbType.Bit).Direction = ParameterDirection.ReturnValue;
sqlComm.Parameters.Add("@UNCFolderPath");
sqlComm.Parameters.Add("@FileType");
foreach (var directval in strLocalSubFolderList)
{
foreach ( var typeval in typesList)
{
sqlComm.Parameters["@UNCFolderPath"].Value = directval;
sqlComm.Parameters["@FileType"].Value = typeval;
sqlComm.ExecuteNonQuery();
// ...if files not transferred, then transfer them
}
}
}
我在 foreach 循环中有这段代码,它为每个文件夹调用 sql 函数
foreach (string strCurrentFolder in strLocalSubFolderList)
{
SqlCommand sqlComm1 = new SqlCommand("dbo.fnChkXfer", _sqlConn);
sqlComm1.CommandType = CommandType.StoredProcedure;
sqlComm1.Parameters.Add("@FileXferred",SqlDbType.Bit).Direction = ParameterDirection.ReturnValue;
sqlComm1.Parameters.AddWithValue("@UNCFolderPath", strCurrentFolder);
sqlComm1.Parameters.AddWithValue("@FileType", "Type 1");
...if files not transferred, then transfer them
SqlCommand sqlComm2 = new SqlCommand("dbo.fnChkXfer", _sqlConn);
sqlComm2.CommandType = CommandType.StoredProcedure;
sqlComm2.Parameters.Add("@FileXferred",SqlDbType.Bit).Direction = ParameterDirection.ReturnValue;
sqlComm2.Parameters.AddWithValue("@UNCFolderPath", strCurrentFolder);
sqlComm2.Parameters.AddWithValue("@FileType", "Type 2");
...if files not transferred, then transfer them
SqlCommand sqlComm3 = new SqlCommand("dbo.fnChkXfer", _sqlConn);
sqlComm3.CommandType = CommandType.StoredProcedure;
sqlComm3.Parameters.Add("@FileXferred",SqlDbType.Bit).Direction = ParameterDirection.ReturnValue;
sqlComm3.Parameters.AddWithValue("@UNCFolderPath", strCurrentFolder);
sqlComm3.Parameters.AddWithValue("@FileType", "Type 3");
...if files not transferred, then transfer the
}
它工作正常,但是有很多重复的代码,这可能是不必要的。
是否可以在循环外创建 SqlCommand 对象并 "reset" 仅在循环中创建参数值?我怎样才能重用这个对象? (请非常具体)。
或者我应该继续将这段代码包含在循环中,每次迭代使用不同的数据执行 fn 3 次?每次都几乎完全相同时,继续重新创建 SqlCommand 对象似乎效率低下。
您绝对可以(甚至应该)在循环外创建 SqlCommand
,并随着循环的进行不断更改参数。为此,您需要在添加参数时存储它们,然后在循环中设置它们的值。您还应该在完成命令后关闭命令,最好以某种自动方式关闭。 using
语句是最常见的选择。
using (var cmd = new SqlCommand("dbo.fnChkXfer", _sqlConn)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@FileXferred",SqlDbType.Bit).Direction = ParameterDirection.ReturnValue;
var p2 = cmd.Parameters.AddWithValue("@UNCFolderPath", DbType.Varchar, 32); // << Set the correct size
var typeParam = cmd.Parameters.AddWithValue("@FileType", , DbType.Varchar, 32);
foreach (string strCurrentFolder in strLocalSubFolderList) {
p2.Value = strCurrentFolder;
foreach (var typeVal in new[] {"Type 1", "Type 2", ...}) {
typeParam.Value = typeVal;
... // Set values of the remaining parameters
... // Use your command as needed
}
}
}
// After this point all three commands will be closed automatically
我将您的列表更改为字典以添加文件类型。如果要重用同一个 sqlcommand 对象,则需要在每次循环迭代中执行它。所以我添加了那部分。您可能想尝试一下,也赶上那里。
Dictionary<string,string> strLocalSubFolderDict = new Dictionary<string, string>();
strLocalSubFolderDict.Add( "Type 1", "Directory 1");
strLocalSubFolderDict.Add( "Type 2", "Directory 2");
strLocalSubFolderDict.Add( "Type 3", "Directory 3");
using (SqlCommand sqlComm = new SqlCommand("dbo.fnChkXfer", _sqlConn))
{
sqlComm.CommandType = CommandType.StoredProcedure;
sqlComm.Parameters.Add("@FileXferred", SqlDbType.Bit).Direction = ParameterDirection.ReturnValue;
sqlComm.Parameters.Add("@UNCFolderPath");
sqlComm.Parameters.Add("@FileType");
foreach (var val in strLocalSubFolderDict)
{
sqlComm.Parameters["@UNCFolderPath"].Value = val.Value;
sqlComm.Parameters["@FileType"].Value = val.Key;
sqlComm.ExecuteNonQuery();
// ...if files not transferred, then transfer them
}
}
在此代码中,对象是在循环外创建的,foreach 中唯一的更改是对对象参数的值进行更改。
另一方面,我不太确定您使用 FileXferred return 参数做什么。你在什么地方用过它吗?
更新
这是每个目录都应用了所有文件类型的代码。
List<string> strLocalSubFolderList = new List<string>();
List<string> typesList = new List<string>();
typesList.Add("Type 1");
typesList.Add("Type 2");
typesList.Add("Type 3");
using (SqlCommand sqlComm = new SqlCommand("dbo.fnChkXfer", _sqlConn))
{
sqlComm.CommandType = CommandType.StoredProcedure;
sqlComm.Parameters.Add("@FileXferred", SqlDbType.Bit).Direction = ParameterDirection.ReturnValue;
sqlComm.Parameters.Add("@UNCFolderPath");
sqlComm.Parameters.Add("@FileType");
foreach (var directval in strLocalSubFolderList)
{
foreach ( var typeval in typesList)
{
sqlComm.Parameters["@UNCFolderPath"].Value = directval;
sqlComm.Parameters["@FileType"].Value = typeval;
sqlComm.ExecuteNonQuery();
// ...if files not transferred, then transfer them
}
}
}