参数化查询 - 多对象
Parameterized query - multi objects
我发现 这正是我想要做的。正确的答案似乎完全符合我的要求,但我想做一些不要再次输入所有参数的事情。
我有一个 <Repere>
,我已经为一个简单的参数化查询实现了一个函数:
public MySqlParameter[] GetListMySqlParams()
{
return this.getListMySqlParams();
}
private MySqlParameter[] getListMySqlParams()
{
MySqlParameter[] listParams = new MySqlParameter[]
{
new MySqlParameter("idContract", this.contrat.ID),
new MySqlParameter("contractCode", this.Contrat.Code),
new MySqlParameter("phaseName", this.fase.Name),
new MySqlParameter("assemblyName", this.assembly.Name),
new MySqlParameter("idPhase", this.fase.ID),
new MySqlParameter("idAss", this.assembly.ID),
new MySqlParameter("name", this.name),
new MySqlParameter("priority", this.priority),
new MySqlParameter("quantity", this.quantity),
new MySqlParameter("totalQuantity", this.totalQuantity),
new MySqlParameter("listOperations", this.listOperations.ConvertToString()),
new MySqlParameter("material", this.geometry.Material),
new MySqlParameter("drawing", this.geometry.Drawing),
new MySqlParameter("profile", this.geometry.Profile.Name),
new MySqlParameter("groupeProfil", this.geometry.GroupeProfil),
new MySqlParameter("length", this.geometry.Length),
new MySqlParameter("weightNet", this.geometry.WeightNet),
new MySqlParameter("revision", this.geometry.Revision),
new MySqlParameter("principal", this.principal),
new MySqlParameter("unloadingZone", this.unloadingZone),
new MySqlParameter("executionClass", this.executionClass),
new MySqlParameter("category", this.category.ID),
new MySqlParameter("description", this.description),
new MySqlParameter("workingOrder", this.workingOrder),
new MySqlParameter("isMountingPart", this.isMountingPart),
new MySqlParameter("isPRS", this.isPRS),
new MySqlParameter("idPRS", this.idPRS),
new MySqlParameter("importOk",this.geometry.ImportOk),
};
return listParams;
}
我想要的是使用那里的解决方案,但不必再次键入所有参数(因为我想在多个对象和大量参数上执行此操作)。
这是我目前拥有的:
private void insertListReperes(List<Repere> listReperes)
{
if (this.OpenConnection() == true)
{
using (this.connection)
{
string query = "INSERT INTO [vsteel].detail (ID_CONTRACT,NAME_CONTRACT,NAME_PHASE,NAME_ASS,ID_PHASE,ID_ASS,NAME,NAME_ORI,PRIORITY,QTE,QTE_TOT,OP," +
"MATERIAL,DRAWING,PROFILE,GROUP_PROFILE,LENGTH,WEIGHT,REVISION,PRINCIPAL,UNLOADING_ZONE,EXECUTION_CLASS,ID_CATEGORY,DESCRIPTION,WORKING_ORDER," +
"IS_MOUNTING_PART,IS_PRS,ID_PRS,IMPORT_OK) " +
"VALUES (@idContract,@contractCode,@phaseName,@assemblyName,@idPhase,@idAss,@name,@name,@priority,@quantity,@totalQuantity,@listOperations," +
"@material,@drawing,@profile,@groupeProfil,@length,@weightNet,@revision,@principal,@unloadingZone,@executionClass,@category,@description,@workingOrder," +
"@isMountingPart,@isPRS,@idPRS,@importOk)";
using (MySqlCommand cmd = new MySqlCommand(query.Replace("[vsteel].", ""), connection))
{
for(int i=0;i<listReperes.Count();i++)
{
Repere repere = listReperes[i];
if(i==0)
{
MySqlParameter[] listParams = repere.GetListMySqlParams();
for (int j = 0; j < listParams.Count(); j++)
{
cmd.Parameters.Add(listParams[i]);
}
cmd.Prepare();
cmd.ExecuteNonQuery();
}
else
{
MySqlParameter[] listParams = repere.GetListMySqlParams();
for (int j = 0; j < listParams.Count(); j++)
{
cmd.Parameters[listParams[i].ParameterName].Value = listParams[i].Value;
}
cmd.ExecuteNonQuery();
}
repere.ID = cmd.LastInsertedId;
}
}
}
}
}
但是当我尝试在第 cmd.Parameters[listParams[i].ParameterName].Value = listParams[i].Value;
行设置参数时,它给出了参数已定义的错误。
有没有不复制所有参数名称的方法?
所以,我找到了问题所在。错误并没有在您所说的那一行抛出;它在 cmd.Parameters.Add
行,这更有意义(源代码只会在添加期间抛出“已经存在”)
您写了:
cmd.Parameters.Add(listParams[i]);
你应该写:
cmd.Parameters.Add(listParams[j]);
通过使用 [i]
,您可以重复添加 idContact
的参数,因为 i
在 j
循环
的每次传递中始终为 0
请注意,您在下一行也犯了同样的错误,因此您重复设置了 idContact 的值 21 次
顺便说一下,我提到了一个名为 Dapper 的库(Whosebug 使用它进行所有数据访问),它可以让您的生活变得更简单。如果你写这样的代码:
using var connection = ... //get your connection here
var sql = "INSERT ... VALUES(@parameter, @names, @identical, @to, @your, @object, @properties); SELECT LAST_INSERT_IDENTITY()";
foreach(var v in itemstoinsert)
v.id = connection.ExecuteScalar<int>(sql, v);
这应该是你所需要的..
我发现
我有一个 <Repere>
,我已经为一个简单的参数化查询实现了一个函数:
public MySqlParameter[] GetListMySqlParams()
{
return this.getListMySqlParams();
}
private MySqlParameter[] getListMySqlParams()
{
MySqlParameter[] listParams = new MySqlParameter[]
{
new MySqlParameter("idContract", this.contrat.ID),
new MySqlParameter("contractCode", this.Contrat.Code),
new MySqlParameter("phaseName", this.fase.Name),
new MySqlParameter("assemblyName", this.assembly.Name),
new MySqlParameter("idPhase", this.fase.ID),
new MySqlParameter("idAss", this.assembly.ID),
new MySqlParameter("name", this.name),
new MySqlParameter("priority", this.priority),
new MySqlParameter("quantity", this.quantity),
new MySqlParameter("totalQuantity", this.totalQuantity),
new MySqlParameter("listOperations", this.listOperations.ConvertToString()),
new MySqlParameter("material", this.geometry.Material),
new MySqlParameter("drawing", this.geometry.Drawing),
new MySqlParameter("profile", this.geometry.Profile.Name),
new MySqlParameter("groupeProfil", this.geometry.GroupeProfil),
new MySqlParameter("length", this.geometry.Length),
new MySqlParameter("weightNet", this.geometry.WeightNet),
new MySqlParameter("revision", this.geometry.Revision),
new MySqlParameter("principal", this.principal),
new MySqlParameter("unloadingZone", this.unloadingZone),
new MySqlParameter("executionClass", this.executionClass),
new MySqlParameter("category", this.category.ID),
new MySqlParameter("description", this.description),
new MySqlParameter("workingOrder", this.workingOrder),
new MySqlParameter("isMountingPart", this.isMountingPart),
new MySqlParameter("isPRS", this.isPRS),
new MySqlParameter("idPRS", this.idPRS),
new MySqlParameter("importOk",this.geometry.ImportOk),
};
return listParams;
}
我想要的是使用那里的解决方案,但不必再次键入所有参数(因为我想在多个对象和大量参数上执行此操作)。
这是我目前拥有的:
private void insertListReperes(List<Repere> listReperes)
{
if (this.OpenConnection() == true)
{
using (this.connection)
{
string query = "INSERT INTO [vsteel].detail (ID_CONTRACT,NAME_CONTRACT,NAME_PHASE,NAME_ASS,ID_PHASE,ID_ASS,NAME,NAME_ORI,PRIORITY,QTE,QTE_TOT,OP," +
"MATERIAL,DRAWING,PROFILE,GROUP_PROFILE,LENGTH,WEIGHT,REVISION,PRINCIPAL,UNLOADING_ZONE,EXECUTION_CLASS,ID_CATEGORY,DESCRIPTION,WORKING_ORDER," +
"IS_MOUNTING_PART,IS_PRS,ID_PRS,IMPORT_OK) " +
"VALUES (@idContract,@contractCode,@phaseName,@assemblyName,@idPhase,@idAss,@name,@name,@priority,@quantity,@totalQuantity,@listOperations," +
"@material,@drawing,@profile,@groupeProfil,@length,@weightNet,@revision,@principal,@unloadingZone,@executionClass,@category,@description,@workingOrder," +
"@isMountingPart,@isPRS,@idPRS,@importOk)";
using (MySqlCommand cmd = new MySqlCommand(query.Replace("[vsteel].", ""), connection))
{
for(int i=0;i<listReperes.Count();i++)
{
Repere repere = listReperes[i];
if(i==0)
{
MySqlParameter[] listParams = repere.GetListMySqlParams();
for (int j = 0; j < listParams.Count(); j++)
{
cmd.Parameters.Add(listParams[i]);
}
cmd.Prepare();
cmd.ExecuteNonQuery();
}
else
{
MySqlParameter[] listParams = repere.GetListMySqlParams();
for (int j = 0; j < listParams.Count(); j++)
{
cmd.Parameters[listParams[i].ParameterName].Value = listParams[i].Value;
}
cmd.ExecuteNonQuery();
}
repere.ID = cmd.LastInsertedId;
}
}
}
}
}
但是当我尝试在第 cmd.Parameters[listParams[i].ParameterName].Value = listParams[i].Value;
行设置参数时,它给出了参数已定义的错误。
有没有不复制所有参数名称的方法?
所以,我找到了问题所在。错误并没有在您所说的那一行抛出;它在 cmd.Parameters.Add
行,这更有意义(源代码只会在添加期间抛出“已经存在”)
您写了:
cmd.Parameters.Add(listParams[i]);
你应该写:
cmd.Parameters.Add(listParams[j]);
通过使用 [i]
,您可以重复添加 idContact
的参数,因为 i
在 j
循环
请注意,您在下一行也犯了同样的错误,因此您重复设置了 idContact 的值 21 次
顺便说一下,我提到了一个名为 Dapper 的库(Whosebug 使用它进行所有数据访问),它可以让您的生活变得更简单。如果你写这样的代码:
using var connection = ... //get your connection here
var sql = "INSERT ... VALUES(@parameter, @names, @identical, @to, @your, @object, @properties); SELECT LAST_INSERT_IDENTITY()";
foreach(var v in itemstoinsert)
v.id = connection.ExecuteScalar<int>(sql, v);
这应该是你所需要的..