Объясните, пожалуйста, переменные @FName
, @LName
и тд это тоже что и customersRow[0]
, customersRow[1]
и тд?
using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Updates
{
static class CustomersUpdates
{
private static void ChangeCustomers(DataRow customersRow)
{
string connectionString = @"Data Source=WIN-9VNS32J8479\IVAN;Initial Catalog=ShopDB;Integrated Security=True";
StringBuilder builder = new StringBuilder();
string commandString = "UPDATE Customers " +
"SET FName = @FName," +
"LName = @LName," +
"MName= @Mname," +
"Address1 = @Address1," +
"Address2 = @Address2," +
"City = @City," +
"Phone = @Phone," +
"DateInSystem = @DateInSystem " +
"WHERE CustomerNo = @CustomerNo"; //обновятся все данные независимо от того какие записи изменим.
//Но изменится только та вставка где CustomerNo = нашему входному параметру (видимо который прописан в источнике).
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand cmd = new SqlCommand(commandString, connection);
cmd.Parameters.AddWithValue("CustomerNo", customersRow[0]);
cmd.Parameters.AddWithValue("FName", customersRow[1]);
cmd.Parameters.AddWithValue("LName", customersRow[2]);
cmd.Parameters.AddWithValue("MName", customersRow[3]);
cmd.Parameters.AddWithValue("Address1", customersRow[4]);
cmd.Parameters.AddWithValue("Address2", customersRow[5]);
cmd.Parameters.AddWithValue("City", customersRow[6]);
cmd.Parameters.AddWithValue("Phone", customersRow[7]);
cmd.Parameters.AddWithValue("DateInSystem", customersRow[8]);
cmd.ExecuteNonQuery();
}
customersRow.AcceptChanges();
}
public static void ChangeCustomers(DataTable customers) //перегрузка метода описанного выше
{
foreach (DataRow row in customers.Rows)
{
if (row.RowState == DataRowState.Modified)
ChangeCustomers(row);
}
}
}
}
//EditCustomerDialog
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 Updates
{
public partial class EditCustomerDialog : Form
{
// DataTable table;
DataRow rowToEdit;
public EditCustomerDialog( DataRow rowToEdit)
{
InitializeComponent();
this.rowToEdit = rowToEdit;
//this.table = table;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
rowToEdit["FName"] = textBox1.Text;
rowToEdit["LName"] = textBox2.Text;
rowToEdit["Address1"] = textBox3.Text;
rowToEdit["City"] = textBox4.Text;
rowToEdit["Phone"] = textBox5.Text;
CustomersUpdates.ChangeCustomers(rowToEdit.Table);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.Close();
}
private void EditCustomerDialog_Load(object sender, EventArgs e)
{
textBox1.Text = rowToEdit["FName"].ToString();
textBox2.Text = rowToEdit["LName"].ToString();
textBox3.Text = rowToEdit["Address1"].ToString();
textBox4.Text = rowToEdit["City"].ToString();
textBox5.Text = rowToEdit["Phone"].ToString();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
//Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Updates
{
public partial class Form1 : Form
{
string connectionString = @"Data Source=WIN-9VNS32J8479\IVAN;Initial Catalog=ShopDB;Integrated Security=True";
string commandString = "SELECT * FROM Customers";
DataTable customers = new DataTable("Customers");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlDataAdapter adapter = new SqlDataAdapter(commandString, connectionString);
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
adapter.Fill(customers);
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = customers;
}
private void button1_Click(object sender, EventArgs e)
{
var editDialog = new EditCustomerDialog((dataGridView1.CurrentRow.DataBoundItem as DataRowView).Row);
editDialog.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
try
{
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}