数据未加载到datagrid视图中。我错过了什么?

时间:2013-08-21 13:56:41

标签: sql datagridview ado.net

我认为这是我正在查看代码的时候之一,这一切看起来都很好,因为我的眼睛认为它会是。我需要一组新的眼睛来查看这段代码并告诉我它没有加载到第一个数据网格视图中。感谢您提供的任何帮助。

private DataSet DataSetRentals { get; set; }

public DataRelationForm()
{
   InitializeComponent();
}    

private void DataRelationForm_Load(object sender, EventArgs e)
{
   DataSet relationship = new DataSet("relationship");

   /////
   SqlConnection conn = Database.GetConnection();

   SqlDataAdapter adapter = new SqlDataAdapter("Select * From Car", conn);
   DataSet DataSetRentals = new DataSet("Relationship");

   adapter.FillSchema(DataSetRentals, SchemaType.Source, "Car");
   adapter.Fill(DataSetRentals, "Car");

   adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
   adapter.Fill(DataSetRentals, "Car");

   DataTable Car;
   Car = DataSetRentals.Tables["Car"];

   foreach (DataRow drCurrent in Car.Rows)
   {
      Console.WriteLine("{0} {1}",
      drCurrent["au_fname"].ToString(),
      drCurrent["au_lname"].ToString());
   }

   ////////////////////////////////////
   SqlDataAdapter adapter2 = new SqlDataAdapter("Select * From CarRental", conn);

   adapter.FillSchema(DataSetRentals, SchemaType.Source, "Rentals");
   adapter.Fill(DataSetRentals, "Rentals");

   adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
   adapter.Fill(DataSetRentals, "Rentals");

   DataTable CarRental;
   CarRental = DataSetRentals.Tables["Rentals"];

   foreach (DataRow drCurrent in CarRental.Rows)
   {
      Console.WriteLine("{0} {1}",
      drCurrent["au_fname"].ToString(),
      drCurrent["au_lname"].ToString());
   }

   /////
   SqlDataAdapter adapter3 = new SqlDataAdapter("Select * From Customer", conn);

   adapter.FillSchema(DataSetRentals, SchemaType.Source, "Customer");
   adapter.Fill(DataSetRentals, "Customer");

   adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
   adapter.Fill(DataSetRentals, "Customer");

   DataTable Customer;
   Customer = DataSetRentals.Tables["Customer"];

   foreach (DataRow drCurrent in Customer.Rows)
   {
      Console.WriteLine("{0} {1}",
      drCurrent["au_fname"].ToString(),
      drCurrent["au_lname"].ToString());
   }

   ////////////////////////

   DataSetRentals.Tables.Add(Customer);
   DataSetRentals.Tables.Add(CarRental);
   DataSetRentals.Tables.Add(Car);

   DataRelation Step1 = new DataRelation("Customer2CarR",
   Customer.Columns["CustomerNo"], CarRental.Columns["CustomerNo"]);
   DataSetRentals.Relations.Add(Step1);

   DataRelation Step2 = new DataRelation("CarR2Car",
Car.Columns["CarID"], CarRental.Columns["CarID"]);
   DataSetRentals.Relations.Add(Step2);

   ////////////////////////
   CustomerGrid.DataSource= DataSetRentals.Tables["Customer"];
   CarRGrid.DataSource = DataSetRentals.Tables["CarRental"];
   CarGrid.DataSource = DataSetRentals.Tables["Car"];

   CustomerGrid.SelectionChanged += new EventHandler(Customer_SelectionChanged);
   CarRGrid.SelectionChanged += new EventHandler(CarR_SelectionChanged);
}

private void Customer_SelectionChanged(Object sender, EventArgs e)
{
   if (CustomerGrid.SelectedRows.Count > 0)
   {
      DataRowView selectedRow =
         (DataRowView)CustomerGrid.SelectedRows[0].DataBoundItem;
      DataSetRentals.Tables["CarRental"].DefaultView.RowFilter =
                "CustomerNo = " + selectedRow.Row["CustomerNo"].ToString();
        }

   else
   {

   }
}

private void CarR_SelectionChanged(Object sender, EventArgs e)
{
   if (CarRGrid.SelectedRows.Count > 0)
   {
      DataRowView selectedRow =
         (DataRowView)CarRGrid.SelectedRows[0].DataBoundItem;
      DataSetRentals.Tables["Car"].DefaultView.RowFilter =
         "CarID = " + selectedRow.Row["CarID"].ToString();
   }
}

这是Database.GetConnection()方法的代码:

SqlConnectionStringBuilder stringBuilder = new SqlConnectionStringBuilder();
bool OnUni;

OnUni = (System.Environment.UserDomainName == "SOAC") ? true : false;

stringBuilder.DataSource = (OnUni) ? @"SOACSQLSERVER\SHOLESQLBSC" : "(local)";
stringBuilder.InitialCatalog = "CarRental_P117365";
stringBuilder.IntegratedSecurity = true;

return new SqlConnection(stringBuilder.ConnectionString);

2 个答案:

答案 0 :(得分:2)

看起来您可能忘记拨打SqlConnection.Open()来实际打开连接。我还建议您在using中包装您的连接并在其末尾显式调用SqlConnection.Close(),这样您就不会意外地将其打开:

using(SqlConnection conn = Database.GetConnection())
{
    conn.Open();

    /*
        rest of code here
    */

    conn.Close();
}

有关正确打开/处理SqlConnections的其他一些好的信息/示例,您还可以查看这些SO问题:

答案 1 :(得分:0)

您没有将数据绑定到任何数据库。

尝试这样的事情

            using(SqlConnection conn = Database.GetConnection())
            try
            {
                {
                 ------your code here up to DataTable Car-----
                  DataTable Car;
                 Car = DataSetRentals.Tables["Car"];

                 gridview.Datasource = Car;


                }
            }
            catch (SqlException sqlex )
            {
                string msg = "Fetch Error:";
                msg += sqlex.Message;
                throw new Exception(msg);
            }

Theres无需打开或关闭SQL连接,因为这是使用顶部打开时的using语句完成的,并且当它完成关闭时\处理连接