public void SelectSQLSrvRows(string myConnection,string mySelectQuery) {
// If the connection string is null, use a default.
if(myConnection == "" ) {
myConnection = "Persist Security Info = False;" +
"Initial Catalog = Northwind;Data Source = myDataServer;User ID = myName;" +
"password=* DCiDKnee;";
}
SQLConnection mySQLSrvConn = new SQLConnection(myConnection);
SQLCommand mySQLCommand = new SQLCommand(mySelectQuery);
mySQLSrvConn.Open();
mySQLCommand.ActiveConnection = mySQLSrvConn;
mySQLCommand.ActiveConnection.Close();
}
public void ReadMyData(string myConnString) {
string mySelectQuery = "SELECT OrderID, Customer FROM Orders";
SQLConnection myConnection = new SQLConnection(myConnString);
SQLCommand myCommand = new SQLCommand(mySelectQuery,myConnection);
myConnection.Open();
SQLDataReader myDataReader;
myCommand.Execute(out myDataReader);
// Always call Read before accessing data.
while (myDataReader.Read()) {
Console.WriteLine(myDataReader.GetInt32(0) + ", " + myDataReader.GetString(1));
}
// always call Close when done reading.
myDataReader.Close();
// Close the connection when done with it.
myConnection.Close();
}
---------------
Ce qui vaut la peine d'être fait vaut la peine d'être bien fait