当我逐步执行代码时,正在添加参数,就在执行sql命令时,它会抛出此错误
public static DataTable GetPostings2(string AssetNumberV, string PeriodFromV, string PeriodToV)
{
DataTable dtGetPostings2;
try
{
dtGetPostings2 = new DataTable("GetPostings");
SqlParameter AssetNumber = new SqlParameter("@AssetNumber", SqlDbType.VarChar, 6);
AssetNumber.Value = AssetNumberV;
SqlParameter PeriodFrom = new SqlParameter("@PeriodFrom", SqlDbType.VarChar, 6);
PeriodFrom.Value = PeriodFromV;
SqlParameter PeriodTo = new SqlParameter("@PeriodTo", SqlDbType.VarChar, 6);
PeriodTo.Value = PeriodToV;
SqlCommand scGetPostings2 = new SqlCommand("SELECT * FROM [POSTING] WHERE [ASSET_NO] = @AssetNumber And PERIOD >= @PeriodFrom AND PERIOD <= @PeriodTo ORDER by PERIOD, JOUR_REF, JOUR_LINE", DataAccess.AssetConnection);
SqlDataAdapter sdaGetPostings2 = new SqlDataAdapter();
sdaGetPostings2.SelectCommand = scGetPostings2;
sdaGetPostings2.Fill(dtGetPostings2);
return dtGetPostings2;
}
catch (Exception ex)
{
MessageBox.Show("Error Retriving Posting Details: Processed with this error:" + ex.Message);
return null;
}
}
答案 0 :(得分:3)
列出的代码不会将参数添加到SqlCommand.Parameters collection。例如:
SqlCommand scGetPostings2 = new SqlCommand("SELECT * FROM [POSTING] WHERE [ASSET_NO] = @AssetNumber And PERIOD >= @PeriodFrom AND PERIOD <= @PeriodTo ORDER by PERIOD, JOUR_REF, JOUR_LINE", DataAccess.AssetConnection);
scGetPostings2.Parameters.Add(AssetNumber);
scGetPostings2.Parameters.Add(PeriodFrom );
scGetPostings2.Parameters.Add(PeriodTo );
// Code continues as above
SqlDataAdapter sdaGetPostings2 = new SqlDataAdapter();
// And so on