c# - 在 C# 中存储 SQL 结果

标签 c# sql

我正在尝试运行 SQL 查询来收集数据并写入数组。我希望能够使用该数组稍后运行一些 if 语句,以查看检索到的数据是否满足所需的某些要求。我对 C# 非常陌生,如果有更好的方法,请为我​​指出正确的方向。这是我到目前为止所拥有的:

public static class DBconnect
    { 

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        public static void sqlConnect()

        {
            var qSql = "Select con.FIRSTNAME, Con.LASTNAME, Con.EMAIL, Act.USERFIELD1 'SerialNumber', ACT.ACCOUNT, TTC.SOFTWAREMAINTENANCE, TTC.HARDWAREMAINTENANCE " +
                    "from CONTACT CON Inner join ACCOUNT ACT on ACT.ACCOUNTID = CON.ACCOUNTID " +
                    "Inner join TTCONTRACTS TTC on TTC.ACCOUNTID = ACT.ACCOUNTID " +
                    "Where Con.EMAIL is not null and Act.USERFIELD1 is not null and " +
                    "ISNUMERIC(Act.USERFIELD1) = 1 and con.EMAIL like '%@%'";

            //Creates connection to sql database
            string connectionString = null;
            SqlConnection cnn;
            connectionString = @"Data Source=DESKTOP-MO813OQ\SalesLogic; Initial Catalog=SalesLogix_Pull; Integrated Security = True";
            cnn = new SqlConnection(connectionString);
            //Sql command to run the query's
            SqlCommand query = new SqlCommand(qSql, cnn);

            QueryResults[] allRecords = null;

            using (query)
            {
                cnn.Open();
                using (var reader = query.ExecuteReader())
                {
                    var list = new List<QueryResults>();
                    while (reader.Read())
                        list.Add(new QueryResults
                        {
                            FIRSTNAME = reader.GetString(0),
                            LASTNAME = reader.GetString(1),
                            EMAIL = reader.GetString(2),
                            SERIALNUMBER = reader.GetInt32(3),
                            ACCOUNT = reader.GetString(4),
                            USERFIELD1 = reader.GetInt32(5),
                            SOFTWAREMAINTENANCE = reader.GetBoolean(6),
                            HARDWAREMAINTENANCE = reader.GetBoolean(7)
                        });
                    allRecords = list.ToArray();
                }

                try
                {
                    cnn.Close();
                    MessageBox.Show("Connection Closed");
                }
                catch (Exception)
                {
                    MessageBox.Show("Error has occured.");
                }
            }


        }


    }

class QueryResults
    {
        public string  FIRSTNAME { get; set; }

        public string LASTNAME { get; set; }

        public string EMAIL { get; set; }

        public int USERFIELD1 {get; set;}

        public int SERIALNUMBER { get; set; }

        public string ACCOUNT { get; set; }

        public bool SOFTWAREMAINTENANCE { get; set; }

        public bool HARDWAREMAINTENANCE { get; set; }
    }

当我运行这个时,我通常会收到这些错误:

这部分代码的 System.Data.dll 中发生了类型为“System.InvalidCastException”的未处理异常:

list.Add(new QueryResults
                        {
                            FIRSTNAME = reader.GetString(0),
                            LASTNAME = reader.GetString(1),
                            EMAIL = reader.GetString(2),
                            SERIALNUMBER = reader.GetInt32(3),
                            ACCOUNT = reader.GetString(4),
                            USERFIELD1 = reader.GetInt32(5),
                            SOFTWAREMAINTENANCE = reader.GetBoolean(6),
                            HARDWAREMAINTENANCE = reader.GetBoolean(7)
                        });

任何帮助或指示将不胜感激!

最佳答案

您的某些数据列可能不会返回预期的输出,例如空值。

试试这个

if(!reader.IsDBNull(colIndex))
   return reader.GetString(colIndex); 
else 
   return string.Empty;

如果这不起作用,您可以尝试调试哪一列有问题并向我提供更多信息。

希望这有帮助。

关于c# - 在 C# 中存储 SQL 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38470961/

相关文章:

c# - 多元素到多行

c# - 控制流从模型返回而不进入 Controller 操作

sql - TSQL 更新时自动增量

c# - 强类型 DataContext 如何工作?

mysql - 为什么当两个字段都包含时此 SQL 不起作用

MySQL查询连接超过1个相关表

c# - 从 c++ DLL 和 c# 中释放 SAFEARRAY

C# - 如何从图像中删除除黑色以外的所有颜色

c# - 为什么 AutoFac 的 AsImplementedInterfaces 在另一种类型上会破坏我的解析器?

mysql - SQL "successful"但不更新数据库?