我无法使用C#中的存储过程将数据插入SQL Server - 请帮忙。
string sqlConnectionString = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;
using (SqlConnection sqlConnection = new SqlConnection(sqlConnectionString))
{
using (SqlCommand command = new SqlCommand("AddFromExcel", sqlConnection))
{
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 0;
try
{
sqlConnection.Open();
SqlParameter parameter = new SqlParameter();
for (int i = 0; i < dtExport.Rows.Count ; i++)
{
command.Parameters.Clear();
SqlParameter parameter1 = new SqlParameter();
parameter1.ParameterName = "@FamilyName";
parameter1.SqlDbType = SqlDbType.NVarChar;
parameter1.Direction = ParameterDirection.Input;
var value = Convert.ToString(dtExport.Rows[i]["Family"]);
command.Parameters.AddWithValue("@FamilyName", value);
SqlParameter parameter2 = new SqlParameter();
parameter2.ParameterName = "@ExecutionName";
parameter2.SqlDbType = SqlDbType.NVarChar;
parameter2.Direction = ParameterDirection.Input;
parameter2.Value = Convert.ToString(dtExport.Rows[i]["Execution"]);
command.Parameters.AddWithValue("@ExecutionName", value);
SqlParameter parameter3 = new SqlParameter();
parameter3.ParameterName = "@VariantCode";
parameter3.SqlDbType = SqlDbType.NVarChar;
parameter3.Direction = ParameterDirection.Input;
parameter3.Value = Convert.ToString(dtExport.Rows[i]["VariantCode"]); //this is column name in Excel
command.Parameters.AddWithValue("@VariantCode", value);
SqlParameter parameter4 = new SqlParameter();
parameter4.ParameterName = "@FamilyDenomination";
parameter4.SqlDbType = SqlDbType.NVarChar;
parameter4.Direction = ParameterDirection.Input;
parameter4.Value = Convert.ToString(dtExport.Rows[i]["Family Denomination"]);
command.Parameters.AddWithValue("@FamilyDenomination", value);
SqlParameter parameter5 = new SqlParameter();
parameter4.ParameterName = "@ExcecutionDenomination";
parameter4.SqlDbType = SqlDbType.NVarChar;
parameter4.Direction = ParameterDirection.Input;
parameter4.Value = Convert.ToString(dtExport.Rows[i]["Execution Denomination"]);
command.Parameters.AddWithValue("@ExcecutionDenomination", value);
SqlParameter parameter6 = new SqlParameter();
parameter4.ParameterName = "@ReleasDatestring";
parameter4.SqlDbType = SqlDbType.NVarChar;
parameter4.Direction = ParameterDirection.Input;
parameter4.Value = Convert.ToString(dtExport.Rows[i]["Release Date"]);
command.Parameters.AddWithValue("@ReleasDatestring", value);
SqlParameter parameter7 = new SqlParameter();
parameter7.ParameterName = "@Validfromstring";
parameter7.SqlDbType = SqlDbType.NVarChar;
parameter7.Direction = ParameterDirection.Input;
parameter7.Value = Convert.ToString(dtExport.Rows[i]["Valid From"]);
command.Parameters.Add(parameter7 );
command.ExecuteNonQuery();
}
}
catch (SqlCeException ex)
{
string w = ex.Message.ToString();
//MessageBox.Show("Error",
// "Error in Write to .: "
// + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
}
}
}
这里存储过程工作正常,即使我在运行程序时没有收到任何错误。 但数据没有插入
答案 0 :(得分:1)
在代码中使用command.Parameters.Add(Parameter_Name)
,例如
for (int i = 0; i < dtExport.Rows.Count ; i++)
{
command.Parameters.Clear();
SqlParameter parameter1 = new SqlParameter();
parameter1.ParameterName = "@FamilyName";
parameter1.SqlDbType = SqlDbType.NVarChar;
parameter1.Direction = ParameterDirection.Input;
parameter1.Value = Convert.ToString(dtExport.Rows[i]["Family"]);
command.Parameters.Add(parameter1);
SqlParameter parameter2 = new SqlParameter();
parameter2.ParameterName = "@ExecutionName";
parameter2.SqlDbType = SqlDbType.NVarChar;
parameter2.Direction = ParameterDirection.Input;
parameter2.Value = Convert.ToString(dtExport.Rows[i]["Execution"]);
command.Parameters.Add(parameter2);
SqlParameter parameter3 = new SqlParameter();
parameter3.ParameterName = "@VariantCode";
parameter3.SqlDbType = SqlDbType.NVarChar;
parameter3.Direction = ParameterDirection.Input;
parameter3.Value = Convert.ToString(dtExport.Rows[i]["VariantCode"]); //this is column name in Excel
command.Parameters.Add(parameter3);
SqlParameter parameter4 = new SqlParameter();
parameter4.ParameterName = "@FamilyDenomination";
parameter4.SqlDbType = SqlDbType.NVarChar;
parameter4.Direction = ParameterDirection.Input;
parameter4.Value = Convert.ToString(dtExport.Rows[i]["Family Denomination"]);
command.Parameters.Add(parameter4);
SqlParameter parameter5 = new SqlParameter();
parameter5.ParameterName = "@ExcecutionDenomination";
parameter5.SqlDbType = SqlDbType.NVarChar;
parameter5.Direction = ParameterDirection.Input;
parameter5.Value = Convert.ToString(dtExport.Rows[i]["Execution Denomination"]);
command.Parameters.Add(parameter5);
SqlParameter parameter6 = new SqlParameter();
parameter6.ParameterName = "@ReleasDatestring";
parameter6.SqlDbType = SqlDbType.NVarChar;
parameter6.Direction = ParameterDirection.Input;
parameter6.Value = Convert.ToString(dtExport.Rows[i]["Release Date"]);
command.Parameters.AddWithValue(parameter6);
SqlParameter parameter7 = new SqlParameter();
parameter7.ParameterName = "@Validfromstring";
parameter7.SqlDbType = SqlDbType.NVarChar;
parameter7.Direction = ParameterDirection.Input;
parameter7.Value = Convert.ToString(dtExport.Rows[i]["Valid From"]);
command.Parameters.Add(parameter7);
command.ExecuteNonQuery();
}