1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
namespace WindowsApplication_testMySql
{
public partial class Form1 : Form
{
// variables locales
DataTable table=null;
private void InitializeDataGridView()
{
try
{
// Set up the DataGridView.
dataGridView1.Dock = DockStyle.None;
// Automatically generate the DataGridView columns.
dataGridView1.AutoGenerateColumns = true;
// Set up the data source.
table = GetData("Select * From etats");
bindingSource1.DataSource = table;
dataGridView1.DataSource = bindingSource1;
// Automatically resize the visible rows.
dataGridView1.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
// Set the DataGridView control's border.
dataGridView1.BorderStyle = BorderStyle.Fixed3D;
// Put the cells in edit mode when user enters them.
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
bindingNavigator1.BindingSource = bindingSource1;
mySqlDataAdapter1.Update(table);
}
catch (MySqlException)
{
MessageBox.Show("To run this sample replace connection.ConnectionString" +
" with a valid connection string to a Northwind" +
" database accessible to your system.", "ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
System.Threading.Thread.CurrentThread.Abort();
}
}
private static DataTable GetData(string sqlCommand)
{
string connectionString = "Server=test_1;Database=reseau;Uid=root;Pwd=;";
MySqlConnection northwindConnection = new MySqlConnection(connectionString);
MySqlCommand command = new MySqlCommand(sqlCommand, northwindConnection);
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = command;
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(table);
return table;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitializeDataGridView();
}
private void button1_Click(object sender, EventArgs e)
{
mySqlDataAdapter1.Update(table);// !!! ?
}
}
} |
Partager