我正在使用MS Access2010 DataBase处理Visual Studio2012 C#。我试图做一个插入,它给了我一个错误,说字段的数据类型是错误的所以我开始一个接一个,在第二个得到“容量超过错误”。谷歌搜索它,找不到任何东西。该错误出现在 result = cmd.ExecuteNonQuery();
public int insertEquipamento(string codigo, int visivel, string freqCal, string fornecedor,
string modelo, string nSerie, string gamaMedida, DateTime dataAquis, string resolucao,
string criterioAceit, string marca)
{
OleDbConnection l = OleDbConnectionDAO.createConnection();
int result = 0;
try
{
l.Open();
OleDbCommand cmd = l.CreateCommand();
cmd.Parameters.Add(new OleDbParameter("@Codigo", codigo));
cmd.Parameters.Add(new OleDbParameter("@visivel", visivel));
cmd.Parameters.Add(new OleDbParameter("@Fabricante", fornecedor));
cmd.Parameters.Add(new OleDbParameter("@Modelo", modelo));
cmd.Parameters.Add(new OleDbParameter("@NumSerie", nSerie));
cmd.Parameters.Add(new OleDbParameter("@DataAq", dataAquis));
//cmd.Parameters.Add(new OleDbParameter("@Entidade", entidadeCal));
cmd.Parameters.Add(new OleDbParameter("@GamaMedida", gamaMedida));
cmd.Parameters.Add(new OleDbParameter("@Precisao", resolucao));
cmd.Parameters.Add(new OleDbParameter("@FreqCal", freqCal));
cmd.Parameters.Add(new OleDbParameter("@critAceit", criterioAceit));
cmd.Parameters.Add(new OleDbParameter("@marca", marca));
cmd.CommandText = "INSERT INTO [Tabela Equipamentos 2012] (" +
"Codigo, [Plano Calibração]" +
//", Fabricante, Modelo, [NúmeroDeSérie]"+
//", [Data Aquisição], [Gama Medida] "+
// ", [Precisão], [Frequência Calibração], [Critério Aceitação], marca" +
") "+
"VALUES " +
"( " +
"'@Codigo', '@visivel'" +
//", '@Fabricante', '@Modelo', '@NumSerie'"+
//", '@DataAq', '@GamaMedida' " +
//", '@Precisao', '@FreqCal', '@critAceit', '@marca'" +
")";
result = cmd.ExecuteNonQuery();
l.Close();
}
catch (Exception ex)
{
l.Close();
System.Diagnostics.Debug.WriteLine("DAO Exception: " + ex.Message);
return result;
}
return result;
}
第二个变量“ visivel ”是一个包含值1或0的int。我尝试在 CommandText <上替换 @visivel / em> ?, 0 , 1 ,当它是任务标记时,它也会显示错误,但如果它是它运行的数字没有任何问题(我在调试中检查了值, visivel 变量是1或0)。 那么为什么这个错误出现了,我可以做些什么来解决它?
答案 0 :(得分:1)
您无需为参数添加Single Quote
:
试试这个:
cmd.CommandText = "INSERT INTO [Tabela Equipamentos 2012] (Codigo, [Plano Calibração]) VALUES (@Codigo, @visivel)";
您需要添加Command Parameters
Command String
cmd.Parameters.Add(new OleDbParameter("@Codigo", codigo));
cmd.Parameters.Add(new OleDbParameter("@visivel", visivel));