我正在使用2017年的微软视觉工作室社区
我的数据库来自MS Access(Microsoft.Jet.OLEDB.4.0)
我认为我的查询在chkNum时存在冲突
这些是我的样本选择查询,总共4个。
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
if (frmTopic.chkRndQ == true && frmTopic.chkNum == false)
{
chkQ = true;
command.CommandText = @"SELECT TOP " + numR + " QUESTION,C1,C2,C3,C4,ANSWER,CONTENT FROM " + tab + " WHERE CONTENT IN ('" + top + "') ORDER BY RND(-(1000*ID)*Time())";
remain = frmTopic.randNo;
num = numR;
}
if (frmTopic.chkRndQ == false && frmTopic.chkNum == false)
{
chkQ = false;
command.CommandText = @"SELECT TOP " + numR + " QUESTION,C1,C2,C3,C4,ANSWER,CONTENT FROM " + tab + " WHERE CONTENT IN ('" + top + "')";
remain = frmTopic.randNo;
num = numR;
}
if (frmTopic.chkRndQ == true && frmTopic.chkNum == true)
{
chkQ = true;
command.CommandText = @"SELECT TOP " + numX + " QUESTION,C1,C2,C3,C4,ANSWER,CONTENT,NUM FROM " + tab + " WHERE CONTENT IN ('" + top + "') AND NUM BETWEEN " + num1 + " AND " + num2 + " ORDER BY RND(-(1000*ID)*Time())";
remain = frmTopic.no;
num = numX;
}
if (frmTopic.chkRndQ == false && frmTopic.chkNum == true)
{
chkQ = false;
command.CommandText = @"SELECT TOP " + numX + " QUESTION,C1,C2,C3,C4,ANSWER,CONTENT,NUM FROM " + tab + " WHERE CONTENT IN ('" + top + "') AND NUM BETWEEN " + num1 + " AND " + num2 + "";
remain = frmTopic.no;
num = numX;
}
OleDbDataAdapter dAdap = new OleDbDataAdapter(command);
dTable = new DataTable();
dAdap.Fill(dTable);
currentRow = 0;
lblNo.Text = currentNo.ToString();
nextRow();
connection.Close();
当我运行程序时,查询应根据条件选择numR或numX数据。
如果我选择检查chkNum,请输入num1和num2。问题是,如果我选择取消选中chkNum,它将只查询先前的numX数据而不是numR数据。但如果我再次检查chkNum并输入num1和num2,或者我只是不选中chkNum,它会正确查询。
与chkNum的不同之处在于它只会从表格中的一个内容中选择指定的n个数据。如果未选中,它将从表格内的多个内容中随机选择指定的n个数据。