Почему не работает UPDATE?
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 System.Data.OleDb;
using System.Configuration;
namespace DataBase
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
AutoPartsDataGridView.DataSource = GetAutoPartsList();
}
private DataTable GetAutoPartsList()
{
DataTable dtAutoParts = new DataTable();
string connString = ConfigurationManager.ConnectionStrings["DataBase.Properties.Settings.Database1ConnectionString"].ConnectionString;
using (OleDbConnection con = new OleDbConnection(connString))
{
using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM AutoParts", con))
{
con.Open();
OleDbDataReader reader = cmd.ExecuteReader();
dtAutoParts.Load(reader);
con.Close();
//dtAutoParts.Columns[0].ColumnMapping = MappingType.Hidden;
}
}
return dtAutoParts;
}
private void AutoPartsDataGridView_CellValueChanged_1(object sender, DataGridViewCellEventArgs e)
{
string s_id = AutoPartsDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
string col = AutoPartsDataGridView.Columns[e.ColumnIndex].Name;
string data = AutoPartsDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
if (s_id != "")
{
string connString = ConfigurationManager.ConnectionStrings["DataBase.Properties.Settings.Database1ConnectionString"].ConnectionString;
using (OleDbConnection con = new OleDbConnection(connString))
{
string sql = string.Format("UPDATE `AutoParts` SET `{0}` = '{1}' WHERE ID = {2};", col, data, s_id);
MessageBox.Show(sql);
using (OleDbCommand cmd = new OleDbCommand(sql, con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
} else
{
string PartName = AutoPartsDataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();
string PartNumber = AutoPartsDataGridView.Rows[e.RowIndex].Cells[2].Value.ToString();
string Price = AutoPartsDataGridView.Rows[e.RowIndex].Cells[3].Value.ToString();
string partAvaible = AutoPartsDataGridView.Rows[e.RowIndex].Cells[4].Value.ToString();
MessageBox.Show("INSERT");
if (PartName != "" & PartNumber != "" & Price != "")
{
string sql = string.Format("INSERT INTO `AutoParts`(PartName, PartNumber, Price, partAvaible) VALUES({0}, {1}, {2}, {3})", PartName, PartNumber, Price, partAvaible);
}
}
AutoPartsDataGridView.DataSource = GetAutoPartsList();
}
}
}
Ошибки не выдает - просто не пишет в базу.
MsgBox с запросом при этом выводит