Другие языки программирования и технологии

Добрый день. Только начинаю осваивать с#. И вот возник вопрос. Как программно заполнить combobox из таблицы бд mysql?

Кира, для связи с данными нужно использовать 3 свойства Datasource, DataMember, ValueMember
типа такого класс себе сделайте:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;

namespace ShipManager1{
class DBConnect{
public MySqlConnection connection{
set;
get;
}
private string server;
public string database{
set;
get;
}
private string uid;
private string password;

//Constructor
public DBConnect(){
Initialize();
}

//Initialize values
private void Initialize(){
server = "localhost";
database = "capital_shipping";
uid = "root";
password = "12345";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

connection = new MySqlConnection(connectionString);
OpenConnection();
}

//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;
}
}

//Backup
public void Backup(){
try{
DateTime Time = DateTime.Now;
int year = Time.Year;
int month = Time.Month;
int day = Time.Day;
int hour = Time.Hour;
int minute = Time.Minute;
int second = Time.Second;
int millisecond = Time.Millisecond;

//Save file to C:\ with the current date as a filename
string path;
path = "C:\\sqlbackup\\MySqlBackup" + year + "-" + month + "-" + day +
"-" + hour + "-" + minute + "-" + second + "-" + millisecond + ".sql";
StreamWriter file = new StreamWriter(path);

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "mysqldump";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}",
uid, password, server, database);
psi.UseShellExecute = false;

Process process = Process.Start(psi);

string output;
output = process.StandardOutput.ReadToEnd();
file.WriteLine(output);
process.WaitForExit();
file.Close();
process.Close();
}
catch (IOException ex){
MessageBox.Show("Error, unable to backup!");
}
}

public void Restore(){
try{
//Read file backed file from directory
string path;
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = "c:\\sqlbackup\\" ;
DialogResult clickedOK = ofd.ShowDialog();
if (clickedOK != DialogResult.OK) return;
path = ofd.FileName;
StreamReader file = new StreamReader(path);
string input = file.ReadToEnd();
file.Close();

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "mysql";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = false;
psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}",
uid, password, server, database);
psi.UseShellExecute = false;

Process process = Process.Start(psi);
process.StandardInput.WriteLine(input);
process.StandardInput.Close();
process.WaitForExit();
process.Close();
}
catch (IOException ex){
MessageBox.Show("Error, unable to Restore!");
}
}
}
}
Еркебулан Абилов
Еркебулан Абилов
28 402
Лучший ответ