无法在报表查看器中显示所有记录

Unable to get all records displayed in report viewer

[编辑:我添加了数据可视化工具] 我有一个教师注册,使用 SQL 数据库中的插入按钮输入记录

private void insertdata(object sender, RoutedEventArgs e)
        {
            try
            {
                string name = name_field.Text;
                string email = email_field.Text;

                string id = id_field.Text;
                string gender;
                if (male.IsChecked == true)
                {
                    gender = "M";
                }
                else
                    gender = "F";


                if (String.IsNullOrEmpty(dob.Text))
                {
                    string fe = "01/01/1900 12:00:00 AM";
                    dt = Convert.ToDateTime(fe);
                }
                else
                {
                    dt = Convert.ToDateTime(dob.Text);

                }



                var rich1 = new TextRange(this.history.Document.ContentStart, this.history.Document.ContentEnd);

                string teach_history = rich1.Text;

                var rich2 = new TextRange(this.achievement.Document.ContentStart, this.achievement.Document.ContentEnd);

                string teach_achievement = rich2.Text;

                connect();
                con.Open();


                string saved = "insert into teacher_details ([Teacher ID],Name,Gender,Email,[Date of Birth],Employment History,Achievements)values('" + id + "', '" + name + "','" + gender + "','" + email + "','" + dt + "','" + teach_history + "','" + teach_achievement + "')";

                SqlCommand cmd = new SqlCommand(saved, con);

                cmd.ExecuteReader();

                con.Close();

                MessageBox.Show("record is added");
              
            }
            catch(Exception ex)
            {
                MessageBox.Show("error occured " + ex);
            }

我的字段有身份证、性别、姓名、电子邮件、历史、成就、出生日期。我为此做了一个 class 如下:

    class Teacherdetail
    {
        public string id { get; set; }
        public string name { get; set; }
        public string email { get; set; }
        public string gender { get; set; }
        public string history { get; set; }
        public string acheive { get; set; }
        public DateTime dob { get; set; }
    }

在我的 rdlc 报告中,我使用对象作为我的数据集并选择了 Teacherdetail。然后我做了一个 table 如下:

然后我有一个按钮查看报告,我将详细信息调用到名为 teacherreport 的报告查看器中

ReportDataSource reportDataSource = new ReportDataSource();
            connect();
            con.Open();

            SqlDataAdapter adp = new SqlDataAdapter("select [Teacher ID],Name,Gender,email,[Date of Birth],[Employment History],Achievements from teacher_details", con);

            DataTable newtab = new DataTable();
            adp.Fill(newtab);
            reportDataSource.Name = "DataSet1";



            reportDataSource.Value = newtab;

            teacherreport.LocalReport.ReportPath = "C:\Users\Preeti Rawat\Documents\Visual Studio 2012\Projects\STDNT\STDNT\teacherreport.rdlc"; 



            teacherreport.LocalReport.DataSources.Add(reportDataSource);

            teacherreport.LocalReport.Refresh();
            teacherreport.RefreshReport();

我的问题是缺少 id、history、name 和 achieve。

当我调试程序时,在我的数据可视化工具中确实显示信息正在通过。 但是它在程序中仍然没有出现。问题是什么,我该如何解决?谢谢^^

对于您的具体问题,在您的 insertdata 中,您正在调用 ExecuteReader 插入一条新记录,但这是错误的。您需要调用 ExecuteNonQueryINSERT, UPDATE, DELETE 个操作。

此外,在 :

string saved = "insert into teacher_details ([Teacher ID],Name,Gender,Email,[Date of Birth],Employment History,Achievements)values('" + id + "', '" + name + "','" + gender + "','" + email + "','" + dt + "','" + teach_history + "','" + teach_achievement + "')";

这应该 return 一个 SQL 错误,因为 Employment History 不包含在方括号 [].

这是您的方法的修订版。 (我试图保持相同的工作)。

var name = name_field.Text;
var name = name_field.Text;
var email = email_field.Text;
var id = id_field.Text;
var gender = male.IsChecked ? "M" : "F";
var dt = DateTime.TryParse(dob.Text, out DateTime result) ? result : new DateTime(1900,1,1); 
var teach_history = new TextRange(this.history.Document.ContentStart, this.history.Document.ContentEnd).Text;
var teach_achievement = new TextRange(this.achievement.Document.ContentStart, this.achievement.Document.ContentEnd).Text;


const string query = "INSERT INTO teacher_details([Teacher ID],Name,Gender,Email,[Date of Birth],[Employment History],Achievements) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}'); ";      

var saved = string.Format(query, id, name, gender, email, dt.ToString("yyyy-MM-ddThh:mm:ss"), teach_history, teach_achievement)

connect();
con.Open();

using(var cmd = new SqlCommand(saved, con))
{
    cmd.ExcuteNonQuery();
}

con.Close();

MessageBox.Show("record is added");

您应该在 SqlConnection 上使用 using 块或手动处理您的连接。另外,我建议您开始使用 Entity Framework 来代替数据库操作。

对于报告部分,您需要这样做:

var table = new DataTable();

const string query = "SELECT [Teacher ID],Name,Gender,email,[Date of Birth],[Employment History],Achievements from teacher_details";

connect();
con.Open();

var adapter = new SqlDataAdapter(query, con);
    adapter.Fill(table);

var reportDataSource = new ReportDataSource("Dataset1", table);

teacherreport.LocalReport.ReportPath = "C:\Users\Preeti Rawat\Documents\Visual Studio 2012\Projects\STDNT\STDNT\teacherreport.rdlc"; 

teacherreport.LocalReport.DataSources.Clear();  

teacherreport.LocalReport.DataSources.Add(reportDataSource);  

teacherreport.LocalReport.Refresh();

teacherreport.RefreshReport(); 

这不起作用的原因是 class 名称与 table 的字段名称不同。它们必须相同。所以我更改了我的数据库的列名 table 使其看起来像这样:

Teacher_ID,姓名,性别,电子邮箱,Date_of_Birth,Employment_History,成就

我的 class 看起来像这样:

        public int Teacher_ID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string email { get; set; }
        public DateTime Date_of_Birth { get; set; }
        public string Employment_History { get; set; }
        public string Achievements { get; set; }