Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.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# 将数据库中的一些数据显示到datagridview_C#_Ms Access - Fatal编程技术网

C# 将数据库中的一些数据显示到datagridview

C# 将数据库中的一些数据显示到datagridview,c#,ms-access,C#,Ms Access,前。 我从组合框中选择“John”。 datagridview将显示数据库中的数据,该数据库的Author=“John”。 例如,有4本书的作者相同(John),如何将这些数据显示到datagridview command.CommandText = "SELECT * FROM Books WHERE Author='"+Convert.ToString(comboBox1.SelectedItem)+"'"; reader = command.ExecuteReader(); 仅供参考:

前。 我从组合框中选择“John”。 datagridview将显示数据库中的数据,该数据库的
Author=“John”
。 例如,有4本书的作者相同(John),如何将这些数据显示到datagridview

command.CommandText = "SELECT * FROM Books WHERE Author='"+Convert.ToString(comboBox1.SelectedItem)+"'";

reader = command.ExecuteReader();

仅供参考:您的脚本易于SQL注入请在SQL查询中使用。这是对的入侵。但是,此查询是否应该获取其作者为
John
的所有书籍?你确定你的设计是正确的吗?
command.CommandText = "SELECT * FROM Books WHERE Author='"+Convert.ToString(comboBox1.SelectedItem)+"'";

var reader = command.ExecuteReader();

var list = new List<string>();
while(reader.Read())
{
   list.Add(reader["Author"].ToString())
}

datagridview.DataSource = list;
public List<string> GetAuthors(string aurtherName)
{

  using(var con = CreateConnection()//somehow )
  {   
    var command = con.CreateCommand();

    string sql = "SELECT * FROM Books WHERE Author='@value'";
     command.Parameters.AddWithValue("@value", aurtherName);
     command.CommandText = sql ;

    var list = new List<string>();
    while(reader.Read())
    {
       list.Add(reader["Author"].ToString())
    }

     return list;
  }
}
datagridview.DataSource = GetAuthors(comboBox1.SelectedItem);