C# 如何对单独表中的列使用reader.GetDecimal()?

C# 如何对单独表中的列使用reader.GetDecimal()?,c#,desktop-application,sqlcommand,C#,Desktop Application,Sqlcommand,我的数据库中有两个表:学生表和帐户表。在我的应用程序中,我尝试使用reader.GetDecimal()方法从多个表中读取数据,但从第二个表中获取数据时遇到问题 是否可以使用GetDecimal方法执行此操作?或者我是否需要添加到其中一个查询中,以从Accounts表中获取所需内容 数据库表: //Query Student table for bAlertSetup SqlCeCommand AlertQuery = new SqlCeComma

我的数据库中有两个表:学生表和帐户表。在我的应用程序中,我尝试使用reader.GetDecimal()方法从多个表中读取数据,但从第二个表中获取数据时遇到问题

是否可以使用GetDecimal方法执行此操作?或者我是否需要添加到其中一个查询中,以从Accounts表中获取所需内容

数据库表:

            //Query Student table for bAlertSetup
            SqlCeCommand AlertQuery = new SqlCeCommand("SELECT * from Students AND Accounts", conn);
            reader = AlertQuery.ExecuteReader();

            while (reader.Read())
            {
                bSinglePersonAlertSetup = reader.GetBoolean(5);

                if (bSinglePersonAlertSetup == true)
                {
                    int AccountID = reader.GetInt32(7);
                    decimal Threshold = reader.GetDecimal(6);
                    //decimal Total = get decimal from the accounts table where accountID (in the accounts table) = AlertAccountID

                    //See if Students account is below the defined threshold
                    if (Total < Threshold)
                    { 
                        StudentEmailAddress = reader.GetString(3);

                        if (StudentEmailAddress != null)
                        {
                            Console.WriteLine(StudentEmailAddress);
                            mail.To.Add(StudentEmailAddress);

                            //Update bAlertSetup
                            SqlCeCommand UpdateBool = new SqlCeCommand("UPDATE Students set bSendAlert = 0 WHERE UserId = @ID");
                            UpdateBool.Parameters.AddWithValue("@ID", reader.GetInt32(0));
                            UpdateBool.Connection = conn;
                            UpdateBool.ExecuteNonQuery();
                        }
                    }     
                }
帐目

学生

代码:

            //Query Student table for bAlertSetup
            SqlCeCommand AlertQuery = new SqlCeCommand("SELECT * from Students AND Accounts", conn);
            reader = AlertQuery.ExecuteReader();

            while (reader.Read())
            {
                bSinglePersonAlertSetup = reader.GetBoolean(5);

                if (bSinglePersonAlertSetup == true)
                {
                    int AccountID = reader.GetInt32(7);
                    decimal Threshold = reader.GetDecimal(6);
                    //decimal Total = get decimal from the accounts table where accountID (in the accounts table) = AlertAccountID

                    //See if Students account is below the defined threshold
                    if (Total < Threshold)
                    { 
                        StudentEmailAddress = reader.GetString(3);

                        if (StudentEmailAddress != null)
                        {
                            Console.WriteLine(StudentEmailAddress);
                            mail.To.Add(StudentEmailAddress);

                            //Update bAlertSetup
                            SqlCeCommand UpdateBool = new SqlCeCommand("UPDATE Students set bSendAlert = 0 WHERE UserId = @ID");
                            UpdateBool.Parameters.AddWithValue("@ID", reader.GetInt32(0));
                            UpdateBool.Connection = conn;
                            UpdateBool.ExecuteNonQuery();
                        }
                    }     
                }

您应该在查询中正确地联接两个表,并且只选择所需的各个列。因此,您的查询可能如下所示

SELECT st.UserID, st.FirstName, st.LastName, acc.AccountName, acc.AccountTotal
FROM Student st
JOIN Accounts acc
ON st.AlertAccountID = acc.AccountID
你可以通过名字得到AccountTotal,就像这样

decimal total = reader.GetDecimal("AccountTotal");
确保在sql中执行查询,并了解每列的索引,以便为每个逗号分隔的字段提供正确的索引,或将S.*替换为S.[FieldName],并且在查询中始终保留列的顺序

你可以用这个

decimal total = reader.GetDecimal(2); //index column name

“希望”将帮助您尝试以下操作:read.GetDecimal(6).ToString()。您需要在此处编辑查询以从第二个表中获取列。我相信该部分目前的工作方式是正确的。我遇到的问题是从Accounts表中获取相应的“AccountID”和“AccountTotal”。可以通过更改以下行来实现:SqlCeCommand AlertQuery=new SqlCeCommand(“从学生和帐户中选择*”,conn);或者我需要一个全新的查询吗?您不需要在两个表之间执行内部联接吗?基本上,您需要选择Accounts.*FROM Accounts internal JOIN Student ON Accounts.StudentID=Student.UserId。如果我在查询中定义了所需的每一列,例如S.AccountThreshold、S.AlertAccountID。我用于GetDeimal()方法的索引是否与我在查询中列出它的顺序相对应?所以我会使用GetDecimal(0)来查找AccountThreshold?确切地说,如果在查询中S.AccountThreshold排在第一位,那么在索引0I处,在编译时会出现无法将字符串转换为int的错误。有没有办法从列名获取表中的索引?
decimal total = reader.GetDecimal(2); //index column name