我已经在2个表中创建了DatagridviewComboBoxColumns(在本例中为“ moduly”和“ zlecenia”表),并从“ projekty”表中获取了插入值。但是,当我单击“更新”按钮时,它没有保存在mysql数据库中(在projekty表中仅保存ID)。所以我不知道如何保存在数据库中(也许我应该添加一列)。有一个问题:
有我的代码:New Tables.cs
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;
using MySql.Data.MySqlClient;
using System.Collections;
namespace KontrolaBazaDanych
{
public partial class New_Tables : Form
{
public New_Tables()
{
InitializeComponent();
}
MySqlConnection connection;
MySqlDataAdapter adapter, adapter2, adapter3, adapter4;
DataSet ds, ds2, ds3, ds4;
DataTable dt, dt1;
public void New_Tables_Load(object sender, EventArgs e)
{
try
{
connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
adapter2 = new MySqlDataAdapter("SELECT moduly.ID_MODULU, moduly.NAZWA, projekty.NAZWA_PROJEKTU FROM projekt1.moduly INNER JOIN projekt1.projekty ON projekty.ID = moduly.ID_PROJEKTU;", connection);
adapter3 = new MySqlDataAdapter("SELECT zlecenia.ID_ZLECENIA, zlecenia.OPIS_ZLECENIA, zlecenia.NUMER_ZLECENIA, projekty.NAZWA_PROJEKTU FROM projekt1.zlecenia INNER JOIN projekt1.projekty ON projekty.ID = zlecenia.ID_PROJEKTU", connection);
adapter4 = new MySqlDataAdapter("SELECT ID, TRIM(NAZWA_PROJEKTU) AS 'NAZWA PROJEKTU' FROM projekt1.projekty", connection);
connection.Open();
ds2 = new DataSet();
adapter2.Fill(ds2, "moduly");
dataGridView2.DataSource = ds.Tables["moduly"];
ds3 = new DataSet();
adapter3.Fill(ds3, "zlecenia");
dataGridView3.DataSource = ds3.Tables["zlecenia"];
ds4 = new DataSet();
adapter4.Fill(ds4, "projekty");
dataGridView4.DataSource = ds4.Tables["projekty"];
dataGridView2.DataSource = loaddata();
fillcombo();
dataGridView3.DataSource = loaddata2();
fillcombo2();
dataGridView2.Columns[0].Visible = false;
dataGridView3.Columns[0].Visible = false;
dataGridView4.Columns[0].Visible = false;
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
try
{
MySqlCommandBuilder cmbl2 = new MySqlCommandBuilder(adapter2);
adapter2.Update(ds2, "moduly");
MySqlCommandBuilder cmbl3 = new MySqlCommandBuilder(adapter3);
adapter3.Update(ds3, "zlecenia");
MySqlCommandBuilder cmbl4 = new MySqlCommandBuilder(adapter4);
adapter4.Update(ds4, "projekty");
MessageBox.Show("Informacja została zauktualizowana", "Aktualizacja", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dataGridView2_RowEnter(object sender, DataGridViewCellEventArgs e)
{
dataGridView2.Rows[e.RowIndex].Cells[0].Value = e.RowIndex + 1;
}
private void dataGridView2_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
dataGridView2.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void dataGridView3_RowEnter(object sender, DataGridViewCellEventArgs e)
{
dataGridView3.Rows[e.RowIndex].Cells[0].Value = e.RowIndex + 1;
}
private void dataGridView3_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
dataGridView3.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void dataGridView4_RowEnter(object sender, DataGridViewCellEventArgs e)
{
dataGridView4.Rows[e.RowIndex].Cells[0].Value = e.RowIndex + 1;
}
private void dataGridView4_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
dataGridView4.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private DataTable loaddata()
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand cmd;
DataSet ds2 = new DataSet();
string sql = "SELECT * FROM projekt1.moduly";
cmd = new MySqlCommand(sql, connection);
adapter.SelectCommand = cmd;
adapter.Fill(ds2);
dt = ds2.Tables[0];
return dt;
}
private void fillcombo()
{
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.HeaderText = "NAZWA PROJEKTU";
combo.Name = "combo";
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand cmd;
DataSet ds2 = new DataSet();
string sql = "SELECT * FROM projekt1.projekty";
cmd = new MySqlCommand(sql, connection);
adapter.SelectCommand = cmd;
adapter.Fill(ds2);
dt = ds2.Tables[0];
ArrayList row = new ArrayList();
Projekty_advanced.lista_projekty.Clear();
foreach (DataRow dr in dt.Rows)
{
Projekty p = new Projekty();
p.ID = (int)dr.ItemArray[0];
p.NAZWA_PROJEKTU = (string)dr.ItemArray[1];
Projekty_advanced.lista_projekty.Add(p);
}
combo.DataSource = Projekty_advanced.lista_projekty;
combo.DisplayMember = "NAZWA_PROJEKTU";
dataGridView2.Columns.Add(combo);
}
private DataTable loaddata2()
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand cmd;
DataSet ds3 = new DataSet();
string sql = "SELECT * FROM projekt1.zlecenia";
cmd = new MySqlCommand(sql, connection);
adapter.SelectCommand = cmd;
adapter.Fill(ds3);
dt1 = ds3.Tables[0];
return dt1;
}
private void fillcombo2()
{
DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn();
combo2.HeaderText = "NAZWA PROJEKTU";
combo2.Name = "combo";
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand cmd;
DataSet ds3 = new DataSet();
string sql = "SELECT * FROM projekt1.projekty";
cmd = new MySqlCommand(sql, connection);
adapter.SelectCommand = cmd;
adapter.Fill(ds3);
dt1 = ds3.Tables[0];
ArrayList row = new ArrayList();
Projekty_advanced.lista_projekty.Clear();
foreach (DataRow dr in dt.Rows)
{
Projekty p = new Projekty();
p.ID = (int)dr.ItemArray[0];
p.NAZWA_PROJEKTU = (string)dr.ItemArray[1];
Projekty_advanced.lista_projekty.Add(p);
}
combo2.DataSource = Projekty_advanced.lista_projekty;
combo2.DisplayMember = "NAZWA_PROJEKTU";
dataGridView3.Columns.Add(combo2);
}
}
}
答案 0 :(得分:0)
怎么样?
private void btnUpdate_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("Server=your_server_name;Database=your_db_name;Trusted_Connection=True;"))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Courses", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
{
SqlCommandBuilder sqlcmd = new SqlCommandBuilder(da);
DataSet ds = new System.Data.DataSet(); // remove this line
da.Update(this.ds, "Courses");
}
}
}
}
}