Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SqlDataReader不读取除第一行以外的任何行_C#_Ado.net - Fatal编程技术网

C# SqlDataReader不读取除第一行以外的任何行

C# SqlDataReader不读取除第一行以外的任何行,c#,ado.net,C#,Ado.net,stackoverflow新手,也是c初学者 当前正在创建一个表单,该表单从数据库中存储的数据生成条形图。所选记录由pID患者ID和tdate测试日期标识。这些值由用户可以从中选择的两个组合框确定,我遇到的问题是,只有存储在数据库中的第一个和最后一个记录填充条形图 if (radioButtonTestResult.Checked) { foreach (var series in TestResultBarChart.Series) { series.Points.Clear(); } st

stackoverflow新手,也是c初学者

当前正在创建一个表单,该表单从数据库中存储的数据生成条形图。所选记录由pID患者ID和tdate测试日期标识。这些值由用户可以从中选择的两个组合框确定,我遇到的问题是,只有存储在数据库中的第一个和最后一个记录填充条形图

 if (radioButtonTestResult.Checked)
{
foreach (var series in TestResultBarChart.Series)
{
series.Points.Clear();
}
string tdate = comboBox2.Text;
using (SqlConnection connection = new SqlConnection(@"Data Source=               (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MMSEDB.mdf;Integrated    Security=True"))
{
connection.Open();
string sql = "SELECT          T_CLOCK_SCORE,T_LANGUAGE_SCORE,T_RECALL_SCORE,T_REGISTRATION_SCORE,T_ORIENTATION  _SCORE,T_TIME FROM TEST_RESULTS WHERE P_ID='" + pID + "' AND T_DATE='"+ tdate +"'";
 using(SqlCommand command = new SqlCommand(sql, connection))
{ 
command.CommandTimeout = 3600;             
using (SqlDataReader reader =     command.ExecuteReader(CommandBehavior.SequentialAccess))
    { 
while (reader.Read())
        {
              MessageBox.Show("hello4");
                String clockScoreString = reader["T_CLOCK_SCORE"].ToString();
                MessageBox.Show(clockScoreString);
                clockScore = Int32.Parse(clockScoreString);
                String langScoreString = reader["T_LANGUAGE_SCORE"].ToString();
                langScore = Int32.Parse(langScoreString);
                String recallScoreString = reader["T_RECALL_SCORE"].ToString();
                recallScore = Int32.Parse(recallScoreString);
                String regScoreString = reader["T_REGISTRATION_SCORE"].ToString();
                regScore = Int32.Parse(regScoreString);
                String orientScoreString = reader["T_ORIENTATION_SCORE"].ToString();
                orientScore = Int32.Parse(orientScoreString);
                String timeScoreString = reader["T_TIME"].ToString();
                timeScore = Int32.Parse(timeScoreString);
        }
        reader.Close(); 
    }
}  


this.TestResultBarChart.Series["Series1"].Points.AddXY("Clock Score", clockScore);
this.TestResultBarChart.Series["Series1"].Points.AddXY("Language Score",     langScore);
this.TestResultBarChart.Series["Series1"].Points.AddXY("Recall Score", recallScore);
this.TestResultBarChart.Series["Series1"].Points.AddXY("Registration Score", regScore);
this.TestResultBarChart.Series["Series1"].Points.AddXY("Orientation Score", orientScore);


}

        }
以下是数据的图片:

以下是第一条记录工作界面的图片:

我知道这与读者有关,但我无法解决如何正确运行


非常感谢您提供的任何帮助

您正在循环中读取所有返回的值,然后退出循环并仅使用最后一个值来设置您的点。应在循环内移动点设置

....
while (reader.Read())
{
    clockScore = Convert.ToInt32(reader["T_CLOCK_SCORE"]);
    langScore = Convert.ToInt32(reader["T_LANGUAGE_SCORE"]);
    recallScore = Convert.ToInt32(reader["T_RECALL_SCORE"]);
    regScore = Convert.ToInt32(reader["T_REGISTRATION_SCORE"]);
    orientScore = Convert.ToInt32(reader["T_ORIENTATION_SCORE"]);
    timeScore = Convert.ToInt32(reader["T_TIME"]);
    this.TestResultBarChart.Series["Series1"].Points.AddXY("Clock Score", clockScore);
    this.TestResultBarChart.Series["Series1"].Points.AddXY("Language Score",     langScore);
    this.TestResultBarChart.Series["Series1"].Points.AddXY("Recall Score", recallScore);
    this.TestResultBarChart.Series["Series1"].Points.AddXY("Registration Score", regScore);
    this.TestResultBarChart.Series["Series1"].Points.AddXY("Orientation Score", orientScore);            
}
reader.Close(); 
请注意,您的查询是使用字符串连接构建的。这是数据库代码的一个众所周知的问题。永远不要这样做,而要使用

编辑 查看下面的评论,我重复使用参数化查询而不是字符串连接的建议。这不仅可以避免Sql注入黑客,而且您也不会离开工作去理解您的值对数据库引擎的意义

DateTime tDate = Convert.ToDateTime(comboBox2.Text);
......
string sql = @"SELECT 
                   T_CLOCK_SCORE,T_LANGUAGE_SCORE,T_RECALL_SCORE,
                   T_REGISTRATION_SCORE,T_ORIENTATION_SCORE,T_TIME 
               FROM TEST_RESULTS 
               WHERE P_ID=@id AND T_DATE=@date";

using(SqlCommand command = new SqlCommand(sql, connection))
{ 
     command.Parameters.Add("@id", SqlDbType.Int).Value = pID;
     command.Parameters.Add("@date", SqlDbType.Date).Value = tdate;
     command.CommandTimeout = 3600;             
     using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
     { 
          while (reader.Read())
          ....
在本例中,我假设变量pID的类型为integer,变量tDate的类型为DateTime,与数据库字段的类型相匹配。这不会让数据库引擎对您的值产生任何疑问。
当然,如果字段的类型不同,则应相应地更改SqlDbType。

在循环中读取所有返回值,然后退出循环,仅使用最后一个值来设置点。我认为你应该在循环中移动点设置,你的数据集中的所有日期都是唯一的,所以你只能得到一行你的查询。您也可以接受SQL注入供参考。应该只显示一条唯一的记录,所以设置点不应该是问题。我把范围缩小到了从数据库中读取t_日期的问题,就像我将select语句更改为P_ID='+pID+'和T_LANGUAGE='3'时,两个结果行按预期填充表一样。这正是使用字符串连接而不是参数化查询时遇到的问题我尝试使用参数化查询,但仍然不起作用,发现存储的sql日期格式和c日期格式不一样是一个问题,所以我现在已经解决了这个问题。感谢您的帮助和建议日期没有格式。您是否将日期存储为字符串?