System.InvalidOperationException:ExecuteReader:CommandText属性尚未初始化

时间:2013-05-23 07:23:38

标签: c# sql database ado.net

关闭我的程序时,我有这样的错误: System.InvalidOperationException:ExecuteReader:CommandText属性尚未初始化 有人可以帮忙吗?这是我的代码:

 try
        {
            conn = new SqlConnection();
            conn.ConnectionString = @"Data Source=ADMIN-PC\SQLEXPRESS;AttachDbFilename=" +
                                    Environment.CurrentDirectory +
                                    @"\diplom.mdf;Integrated Security=True; User Instance = true";
            conn.Open();

        SqlCommand myCommand = conn.CreateCommand();
        string myComm = "";

        if (comboBox1.SelectedIndex == 0)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Директор'";
        if (comboBox1.SelectedIndex == 1)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Коммерческий директор'";
        if (comboBox1.SelectedIndex == 2)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Супервайзер'";
        if (comboBox1.SelectedIndex == 3)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Мерчендайзер'";
        if (comboBox1.SelectedIndex == 4)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Торговый агент'";
        if (comboBox1.SelectedIndex == 5)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Аналитик'";
        if (comboBox1.SelectedIndex == 6)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Главный бухгалтер'";
        if (comboBox1.SelectedIndex == 7)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Бухгалтер'";
        if (comboBox1.SelectedIndex == 8)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Начальник отдела'";
        if (comboBox1.SelectedIndex == 9)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Оператор'";
        if (comboBox1.SelectedIndex == 10)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Кассир'";
        if (comboBox1.SelectedIndex == 11)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Системный администратор'";
        if (comboBox1.SelectedIndex == 12)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Заведующий складом'";
        if (comboBox1.SelectedIndex == 13)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Кладовщик'";
        if (comboBox1.SelectedIndex == 14)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Грузчик'";
        if (comboBox1.SelectedIndex == 15)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Водитель'";
        if (comboBox1.SelectedIndex == 16)
            myComm = @"SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, 
                 position, brand, model, name_of_department FROM View2 where position = 'Экспедитор'";

        myCommand.CommandText = myComm;
        SqlDataAdapter dataAdapter = new SqlDataAdapter();
        dataAdapter.SelectCommand = myCommand;
        DataSet ds = new DataSet();
        dataAdapter.Fill(ds, "View2");
        dataGridView1.DataSource = ds.Tables["View2"].DefaultView;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        conn.Close();
    } 

2 个答案:

答案 0 :(得分:2)

comboBox1.SelectedIndex不在0-16之间,因此myComm为空字符串。这就是你收到错误的原因。另请考虑将switchdefault命令一起使用。

在执行命令之前,请检查一下。

if(!string.IsNullOrWhiteSpace(myComm))
{
    myCommand.CommandText = myComm;
    SqlDataAdapter dataAdapter = new SqlDataAdapter();
    dataAdapter.SelectCommand = myCommand;
    DataSet ds = new DataSet();
    dataAdapter.Fill(ds, "View2");
    dataGridView1.DataSource = ds.Tables["View2"].DefaultView;
}

答案 1 :(得分:1)

组合框的SelectedIndex存在问题。有人已经说过,如果SelectedIndex不在0到16之间,你就不会初始化commandtext。 我还想为你的代码添加一个优化,除了where条件之外,查询文本总是相同的,所以你可以编写

  string query = "SELECT fam, name, patronymic, date_birth, adress, phone_number, salary, " + 
                 "position, brand, model, name_of_department FROM View2 " + 
                 "where position = @condition";
  string condition = string.Empty;
  switch(combobox1.SelectedIndex)
  {
       case 0:
          condition = "Директор";
          break;
       case 1:
          condition = "Коммерческий директор";
          break;
       ..... and so on 
}

if(!string.IsNullOrWhiteSpace(condition))
{
    myCommand.CommandText = query;
    myCommand.Parameters.AddWithValue("@condition", condition);
    SqlDataAdapter dataAdapter = new SqlDataAdapter();
    dataAdapter.SelectCommand = myCommand;
    DataSet ds = new DataSet();
    dataAdapter.Fill(ds, "View2");
    dataGridView1.DataSource = ds.Tables["View2"].DefaultView;
}