我应该把我的C#if语句放在哪里,对于我的sql查询返回空白的情况?

时间:2013-11-29 15:37:54

标签: c# sql sql-server if-statement sql-server-ce

我在哪里可以使用if语句说:如果sql查询返回空,那么Console.WriteLine("I'm sorry, empty...whatnot");

我不知道如何查看查询结果是否为空。

这是我的代码:

public void IsMovieInStore()
{
    Console.Write("Searh for a movie title: ");
    string title = Console.ReadLine();

    string connectionString = @"Data Source=|DataDirectory|\VideoStoreDB.sdf";
    SqlCeConnection connection = new SqlCeConnection(connectionString);

    SqlCeCommand command = new SqlCeCommand("SELECT Movie.Title, MovieHandler.InStore FROM Movie INNER JOIN MovieHandler ON Movie.MovieCodeLable = MovieHandler.MovieCodeLable WHERE MovieHandler.InStore = 1 AND Movie.Title = @title", connection);
    command.Parameters.AddWithValue("@title", title);

    SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(command);

    DataSet dataSet = new DataSet();
    dataAdapter.Fill(dataSet, "Movie");


    foreach (DataTable dataTable in dataSet.Tables)
    {
        foreach (DataRow row in dataTable.Rows)
        {
            foreach (DataColumn column in dataTable.Columns)
            {
                Console.WriteLine(column.ColumnName + ": " + row[column]);
            }
            Console.WriteLine("-------------------------");
        }
    }
    Console.ReadLine();
}

3 个答案:

答案 0 :(得分:3)

尝试检查表和行计数

//....
dataAdapter.Fill(dataSet, "Movie");

if (0 == dataSet.Tables.Count || 0 == dataSet.Tables[0].Rows.Count)
{
    Console.WriteLine("I'm so lonely");
}

foreach (DataTable dataTable in dataSet.Tables)
//...

答案 1 :(得分:3)

您可以通过将dataAdapter.Fill(dataSet, "Movie");分配给int变量轻松进行检查。 Fill方法返回在数据集中添加或刷新的行数。有关详细信息,请参阅msdn:DataAdapter.Fill Method

int rows = dataAdapter.Fill(dataSet, "Movie");

if(rows > 0)
{
    //process data
}
else
{
    Console.WriteLine("Sorry, no data...");
}

答案 2 :(得分:0)

您不需要使用数据集,请使用数据表:

public void IsMovieInStore()
                {
                    Console.Write("Searh for a movie title: ");
                    string title = Console.ReadLine();

                    string connectionString = @"Data Source=|DataDirectory|\VideoStoreDB.sdf";
                    SqlCeConnection connection = new SqlCeConnection(connectionString);

                    SqlCeCommand command = new SqlCeCommand("SELECT Movie.Title, MovieHandler.InStore FROM Movie INNER JOIN MovieHandler ON Movie.MovieCodeLable = MovieHandler.MovieCodeLable WHERE MovieHandler.InStore = 1 AND Movie.Title = @title", connection);
                    command.Parameters.AddWithValue("@title", title);

                    SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(command);

                    DataTable dataTable = new DataTable();
                    dataAdapter.Fill(dataTable);

        If(dataTable != null && dataTable.Rows.Count>0)
        {
                     foreach (DataRow row in dataTable.Rows)
                        {
                            foreach (DataColumn column in dataTable.Columns)
                            {
                                Console.WriteLine(column.ColumnName + ": " + row[column]);
                            }
                            Console.WriteLine("-------------------------");
                        }
                  }
    }
        else{
          Console.WriteLine("Empty result");
        }
                    Console.ReadLine();
                }