C# 将两个相同的参数传递给access数据库
在C代码中,我试图执行一个查询并将两个参数传递给Access数据库。不幸的是,参数名是相同的,因为我正在传递日期参数。似乎当我超出代码时,第一个参数被第二个参数替换,例如,我正在通过 开始日期为2017年1月1日,结束日期为2017年1月31日 我仍然得到2016年的价值观 下面是我的代码C# 将两个相同的参数传递给access数据库,c#,C#,在C代码中,我试图执行一个查询并将两个参数传递给Access数据库。不幸的是,参数名是相同的,因为我正在传递日期参数。似乎当我超出代码时,第一个参数被第二个参数替换,例如,我正在通过 开始日期为2017年1月1日,结束日期为2017年1月31日 我仍然得到2016年的价值观 下面是我的代码 string ConnString = getConnstring(); string DirectoryPath = getDirectortyPath(year, quater);
string ConnString = getConnstring();
string DirectoryPath = getDirectortyPath(year, quater);
StreamWriter file = new StreamWriter(DirectoryPath);
string StartDate = calculateStartDate(year, quater);
string EndDate = CalculateEndDate(year, quater);
string SqlString = "Select * from TestTable where compl_date >= ? and compl_date <= ?";
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("compl_date", StartDate); // startDate can be 1/1/2017
cmd.Parameters.AddWithValue("compl_date", EndDate); // end date could be 1/31/2017
conn.Open();
using (OleDbDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
try
{
string FullLine = "";
如何获取上述日期范围内的值。像这样尝试
string ConnString = getConnstring();
string DirectoryPath = getDirectortyPath(year, quater);
StreamWriter file = new StreamWriter(DirectoryPath);
string StartDate = calculateStartDate(year, quater);
string EndDate = CalculateEndDate(year, quater);
string SqlString = "Select * from TestTable where compl_date >= @StartDate and compl_date <= @EndDate";
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@StartDate", StartDate);
cmd.Parameters.AddWithValue("@EndDate", EndDate);
conn.Open();
....
我会向流编写器添加另一个using,以确保它也被关闭。开始日期和结束日期使用相同的参数名。因此,当您调用cmd.Parameters.AddWithValuecompl_date时,EndDate;它把结束日期放在两个地方。您需要不同的参数名称。