Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.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# 从数据库中选择一些记录,并使用sqlite数据库通过c显示在数据网格中_C#_Winforms_Sqlite - Fatal编程技术网

C# 从数据库中选择一些记录,并使用sqlite数据库通过c显示在数据网格中

C# 从数据库中选择一些记录,并使用sqlite数据库通过c显示在数据网格中,c#,winforms,sqlite,C#,Winforms,Sqlite,我有一个如下所示的示例数据库,我只想在datagrid中显示红色记录。我有条件让这两个细胞变成红色 样本数据库 例如,我想在“图书编号”列中显示其值小于10的记录 我用下面的代码把它们变成红色 密码 你试过了吗 foreach(DataGridView row in dataGridView1.Rows) { //check whether bookno. column in 'row' is less than 10 //and do something } 我刚刚阅读了

我有一个如下所示的示例数据库,我只想在datagrid中显示红色记录。我有条件让这两个细胞变成红色

样本数据库 例如,我想在“图书编号”列中显示其值小于10的记录

我用下面的代码把它们变成红色

密码 你试过了吗

foreach(DataGridView row in dataGridView1.Rows)
{
     //check whether bookno. column in 'row' is less than 10
     //and do something
}
我刚刚阅读了您的评论,您希望在datagridview中显示之前直接从数据库查询,这是您想要的吗

对于SQLite数据库,您可以使用标准的SQLSELECT语句根据您的条件获取记录。示例从bookNum>10的位置选择*

这将为您提供bookNum大于10的所有记录

首先创建您的连接

SQLiteConnection connection = new SQLiteConnection(connectionPath)
然后是数据适配器

 SQLiteDataAdapter da = new SQLiteDataAdapter(query, connection);
执行表映射(如果有)。 然后打电话给da.Fillds,发行图书

我还注意到您在sql查询中使用As作为列名。实际上,您可以使用tablemappings将数据库列名映射到datatable列名。请参阅。

答案
对于示例,我想在第列图书中显示值小于10的记录。使它们变为红色的条件是什么?您现在是否正在以任何方式读取数据库单元格?如果是,那么您使用的是哪种方法?有很多方法可以做到这一点,SQL中的WHERE子句、Linq、通过foreach将成员从一个集合复制到另一个集合、DataView上的文件服务器,然后各种ORM为您提供了不同的过滤技术,到目前为止,您尝试了什么?@0A0D我使用上面的代码使它们变为红色,但现在我只想显示那些满足条件的记录否我想要像first这样的代码我首先获取数据库中任何行值的列如果该值满足我的条件,那么我想要像那样在datagrid中打印我想要检查数据库中的所有行我的main要求是我只想显示数据库中的过去日期记录我已经更新了我的答案。。。您执行的操作与填充数据集时相同,但使用列条件查询,然后将数据集绑定到datagridview
SQLiteConnection connection = new SQLiteConnection(connectionPath)
 SQLiteDataAdapter da = new SQLiteDataAdapter(query, connection);
public void onlyDueReport()
    {
        List<int> array = new List<int>();
        string connectionPath = @"Data Source=Data\libraryData.dat;Version=3;New=False;Compress=True";
        using (SQLiteConnection connection = new SQLiteConnection(connectionPath))
        {
            SQLiteCommand command = connection.CreateCommand();
            connection.Open();
            string query = "SELECT bookno as 'Book No.',studentId as 'Student ID',  title as 'Title', author as 'Author', description as 'Description', issuedDate as 'Issued Date', dueDate as 'Due Date' FROM issuedBooks";
            command.CommandText = query;
            command.ExecuteNonQuery();
            SQLiteDataAdapter da = new SQLiteDataAdapter(command);
            DataSet ds = new DataSet();
            da.Fill(ds, "issuedBooks");
            int c = ds.Tables["issuedBooks"].Rows.Count;
            if (c > 0)
            {
                for (int row = c; row > 0; row--)
                {
                    string date = (string)(ds.Tables["issuedBooks"].Rows[c - row]["Due Date"]);
                    if (isLate(date))
                    {
                        int a = Convert.ToInt32(ds.Tables["issuedBooks"].Rows[c - row]["Book No."]);
                        array.Add(a);
                    }
                }
            }

            query = "SELECT bookno as 'Book No.',studentId as 'Student ID',  title as 'Title', author as 'Author', description as 'Description', issuedDate as 'Issued Date', dueDate as 'Due Date' FROM issuedBooks WHERE bookno IN (";
            int[] cool = array.ToArray();
            int cou = 0;
            foreach (int a in cool)
            {
                query += a;
                if (cou < cool.Length - 1) { query += ','; }
                cou++;
            }
            query += ")";

            Console.WriteLine(query);
            command.CommandText = query;
            command.ExecuteNonQuery();
            DataSet ds1 = new DataSet();
            da.Fill(ds1, "issuedBooks");
            dataGridView1.DataSource = ds1.Tables["issuedBooks"];
            this.Totals.Text = "";
            Report_Viewer.StatusPText = " Total Pending Books :  " + ds1.Tables["issuedBooks"].Rows.Count; 
        }

    }