
February 27, 2010 14:33 by
Sigenstrøm |
Denne code snippet er til at hente data ud fra en Access database 2003.
Filen skal gemmes som en xml fil og ligge i C: \ Program Files \ Microsoft Visual Studio 8 \ VC # \ Snippets \ 1033 \ Visual C #Eller den mappe som nu måtte passe.!
Og som altid er der sprøgsmål så skriv.!Den færdige xml fil kan hentes her
//The following should be inserted into the top of the page before this snippet works // //using System.Data.OleDb; // //This snippet is made by Jimmy Nielsen @ sigenstrøm.dk / www.xn--sigenstrm-s8a.dk //The code is open source, so long as the code's / author's comments are not removed //If you use my code snippets, feel free to send me a mail or link to my website. //That way I can see how many people are using my snippets. //This is not a requirement ;-) //Converts the selected id to int int id = Set id; //you wish to select data by another field, then you must string the variabel by using this line insted of "int id = Set id;" //string id Set id; OleDbConnection MyConnection = new OleDbConnection(); //Sets the path to the database MyConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|DatabaseName.mdb"; //Here, data is retrieved from the table the selected id" string Mystring = ("Select * from TabelName where Id=" + id + ""); //Use this line if you select data by another field //string Mystring = ("Select * from TabelName where Id='" + id + "'"); OleDbCommand objCommand = new OleDbCommand(Mystring, MyConnection); OleDbDataReader objDataReader = null; try { //Opens the database connection MyConnection.Open(); //The reader objDataReader = objCommand.ExecuteReader(); //While the reader is open do this while (objDataReader.Read() == true) { //Here we come data from the database into text boxes, label or other SetData+= Convert.ToString(objDataReader["Selectcolumn"]); } //Closes the reader objDataReader.Close(); } //Catches the errors catch (Exception exept) { //Prints error Response.Write(exept); } //Closes the connection to the database again MyConnection.Close();
a4d59643-6a51-41e7-9524-93f1aab5da88|1|5.0