string cString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer"; SqlConnection myConnection = new SqlConnection(cString); // Create a SqlDataAdapter. SqlDataAdapter myAdapter = new SqlDataAdapter(); myAdapter.TableMappings.Add("Table", "Suppliers"); myConnection.Open(); SqlCommand myCommand = new SqlCommand("SELECT * FROM Suppliers", myConnection); myCommand.CommandType = CommandType.Text;
myAdapter.SelectCommand = myCommand; Console.WriteLine("The connection is open"); ds = new DataSet("Customers"); myAdapter.Fill(ds); // Create a second Adapter and Command. SqlDataAdapter adpProducts = new SqlDataAdapter(); adpProducts.TableMappings.Add("Table", "Products"); SqlCommand cmdProducts = new SqlCommand("SELECT * FROM Products", myConnection); adpProducts.SelectCommand = cmdProducts; adpProducts.Fill(ds); myConnection.Close(); Console.WriteLine("The connection is closed."); System.Data.DataRelation dr; System.Data.DataColumn dc1; System.Data.DataColumn dc2; // Get the parent and child columns of the two tables. dc1 = ds.Tables["Suppliers"].Columns["SupplierID"]; dc2 = ds.Tables["Products"].Columns["SupplierID"]; dr = new System.Data.DataRelation("suppliers2products", dc1, dc2); ds.Relations.Add(dr);
这是异常信息!!
{}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
把代码放到try里,你再把异常贴出来看看
还有你最好把抛出的异常贴出来
SqlConnection myConnection = new SqlConnection(cString);
// Create a SqlDataAdapter.
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.TableMappings.Add("Table", "Suppliers");
myConnection.Open();
SqlCommand myCommand = new SqlCommand("SELECT * FROM Suppliers",
myConnection);
myCommand.CommandType = CommandType.Text;
myAdapter.SelectCommand = myCommand;
Console.WriteLine("The connection is open");
ds = new DataSet("Customers");
myAdapter.Fill(ds);
// Create a second Adapter and Command.
SqlDataAdapter adpProducts = new SqlDataAdapter();
adpProducts.TableMappings.Add("Table", "Products");
SqlCommand cmdProducts = new SqlCommand("SELECT * FROM Products",
myConnection);
adpProducts.SelectCommand = cmdProducts;
adpProducts.Fill(ds);
myConnection.Close();
Console.WriteLine("The connection is closed.");
System.Data.DataRelation dr;
System.Data.DataColumn dc1;
System.Data.DataColumn dc2;
// Get the parent and child columns of the two tables.
dc1 = ds.Tables["Suppliers"].Columns["SupplierID"];
dc2 = ds.Tables["Products"].Columns["SupplierID"];
dr = new System.Data.DataRelation("suppliers2products", dc1, dc2);
ds.Relations.Add(dr);