using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MySql.Data.MySqlClient; using System.Windows.Forms; namespace physio { class mySql { private MySqlConnection connection; private string server; private string database; private string uid; private string password; public string Name { get; set; } public string Address1 { get; set; } public string Attn { get; set; } public mySql() { Initialize(); } //Initialize values private void Initialize() { server = "XXXXX"; database = "XXXXX"; uid = "XXXXX"; password = "XXXXX"; string connectionString; connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";"; connection = new MySqlConnection(connectionString); } //open connection to database private bool OpenConnection() { try { connection.Open(); return true; } catch (MySqlException ex) { //When handling errors, you can your application's response based //on the error number. //The two most common error numbers when connecting are as follows: //0: Cannot connect to server. //1045: Invalid user name and/or password. switch (ex.Number) { case 0: MessageBox.Show("Cannot connect to server. Contact administrator"); break; case 1045: MessageBox.Show("Invalid username/password, please try again"); break; } return false; } } //Close connection private bool CloseConnection() { try { connection.Close(); return true; } catch (MySqlException ex) { MessageBox.Show(ex.Message); return false; } } public List<object> LoadCustomers() { var listOfCustomers = new List<object>(); string query = "SELECT * FROM customers where id = '1200'"; //Open connection this.OpenConnection(); { //Create Command MySqlCommand cmd = new MySqlCommand(query, connection); //Create a data reader and Execute the command MySqlDataReader dataReader = cmd.ExecuteReader(); //Read the data and store them in the list while (dataReader.Read()) { mySql c = new mySql(); c.Name = dataReader["Name"].ToString(); c.Address1 = dataReader["Address1"].ToString(); c.Attn = dataReader["Attn"].ToString(); listOfCustomers.Add(c); } //close Data Reader dataReader.Close(); //close Connection this.CloseConnection(); //return list to be displayed return listOfCustomers; } } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace physio { public partial class Managed : Form { mySql managed = new mySql(); public Managed() { InitializeComponent(); List<object> customers = new List<object>(); customers.Add(managed.LoadCustomers()); listBox1.DataSource = customers; } } }
Hej Martin,dette er temmelig pinligt, men der var ikke hul igennem til serveren :-/Så det er rettet nu. Dog bliver resultatet nu at der står "samling" i min list<objekt> denne er kaldt i Managed.cs