在教程的过程中没有注意到这一点。即使是我观看的Youtube视频也没有明确说明,当他们“证明”更新正在发生时,他们没有明确表示他们正在检查临时数据库,而不是主要数据库。您是否可以修改您的答案,以包括具体的操作方法?我可能应该说清楚,我对C#和SQL都相当陌
在教程的过程中没有注意到这一点。即使是我观看的Youtube视频也没有明确说明,当他们“证明”更新正在发生时,他们没有明确表示他们正在检查临时数据库,而不是主要数据库。您是否可以修改您的答案,以包括具体的操作方法?我可能应该说清楚,我对C#和SQL都相当陌,c#,sql-server,datagridview,C#,Sql Server,Datagridview,在教程的过程中没有注意到这一点。即使是我观看的Youtube视频也没有明确说明,当他们“证明”更新正在发生时,他们没有明确表示他们正在检查临时数据库,而不是主要数据库。您是否可以修改您的答案,以包括具体的操作方法?我可能应该说清楚,我对C#和SQL都相当陌生。此外,我不明白的是,我看到代码是按我的方式运行的,并且它更新了此人的youtube教程中的SQL数据库。他使用数据集这一事实是原因吗?对不起,想了解更多细节,但我应该在哪里以及如何设置数据集的值“updatedValue和idOfRowTo
在教程的过程中没有注意到这一点。即使是我观看的Youtube视频也没有明确说明,当他们“证明”更新正在发生时,他们没有明确表示他们正在检查临时数据库,而不是主要数据库。您是否可以修改您的答案,以包括具体的操作方法?我可能应该说清楚,我对C#和SQL都相当陌生。此外,我不明白的是,我看到代码是按我的方式运行的,并且它更新了此人的youtube教程中的SQL数据库。他使用数据集这一事实是原因吗?对不起,想了解更多细节,但我应该在哪里以及如何设置数据集的值“updatedValue和idOfRowToUpdate?我是否需要告诉它以增量方式抛出并提取每行中的值并发送它来更新表,或者是否有方法验证该值是否已从最初加载的值更改,然后仅更新该行上信息的索引?最后,我尝试使用DataSet而不是DataTable,因为我找到了一些其他论坛和指向有相同问题的人的链接,并将其编码为与我几乎相同的代码,其他人说使用DataSet会起作用-它不会起作用。好吧,所以我尝试了几种不同的方式,但没有起作用。因此,我将更新主要问题以更详细地解释。
public partial class Form1 : Form
{
SqlConnection con;
SqlDataAdapter sda;
DataTable dt;
SqlCommandBuilder scb;
private int rowIndex = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
con = new SqlConnection(Properties.Settings.Default.SchoolConnectionString);
con.Open();
sda = new SqlDataAdapter("SELECT * FROM School", con);
dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpButton_Click(object sender, EventArgs e)
{
try
{
scb = new SqlCommandBuilder(sda);
sda.Update(dt);
MessageBox.Show("Information Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//button to refresh the data without need to close the app
private void RefButton_Click(object sender, EventArgs e)
{
try
{
con = new SqlConnection(Properties.Settings.Default.SchoolConnectionString);
con.Open();
sda = new SqlDataAdapter("SELECT * FROM School", con);
dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void CloseButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
//BROKEN - supposed to bring up a menu item to delete the row
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.dataGridView1.Rows[e.RowIndex].Selected = true;
this.rowIndex = e.RowIndex;
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1];
this.contextMenuStrip1.Show(this.dataGridView1, e.Location);
contextMenuStrip1.Show(Cursor.Position);
}
}
private void contextMenuStrip1_Click(object sender, CancelEventArgs e)
{
if (!this.dataGridView1.Rows[this.rowIndex].IsNewRow)
{
this.dataGridView1.Rows.RemoveAt(this.rowIndex);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
con = new SqlConnection(Properties.Settings.Default.SchoolConnectionString);
con.Open();
sda = new SqlDataAdapter("SELECT * FROM School", con);
ds = new DataSet();
sda.Fill(ds, "e");
dataGridView1.DataSource = ds.Tables["e"];
}
catch (Exception ex)
{
MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpButton_Click(object sender, EventArgs e)
{
try
{
scb = new SqlCommandBuilder(sda);
sda.Update(ds, "e");
MessageBox.Show("Information Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
con = new SqlConnection(Properties.Settings.Default.SchoolConnectionString);
con.Open();
sda = new SqlDataAdapter("SELECT * FROM School", con);
dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpButton_Click(object sender, EventArgs e)
{
try
{
scb = new SqlCommandBuilder(sda);
newDT = dt.GetChanges();
if (newDT != null)
{
sda.Update(newDT);
}
MessageBox.Show("Information Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
con = new SqlConnection(Properties.Settings.Default.SchoolConnectionString);
con.Open();
sda = new SqlDataAdapter("SELECT * FROM School", con);
dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpButton_Click(object sender, EventArgs e)
{
try
{
newDT = dt.GetChanges();
if (newDT != null)
{
sda.Update(newDT);
}
MessageBox.Show("Information Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
sda.UpdateCommand = "UPDATE TABLE SET ..."
private void UpButton_Click(object sender, EventArgs e)
{
try
{
using(con = new SqlConnection(Properties.Settings.Default.SchoolConnectionString))
{
con.Open();
string sqlCommand = "Update (Table) set value=@Value where id=@ID";
SqlCommand cmd = new SqlCommand(sqlCommand, con);
cmd.Parameters.AddWithValue("@Value", updatedValue);
cmd.Parameters.AddWithValue("@ID", idOfRowToUpdate);
int rowsAffected = cmd.ExecuteNonQuery();
if(rowsAffected == 1)
{
MessageBox.Show("Information Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
con.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
try
{
DataTable newDT = dt.GetChanges();
if(newDT != null)
{
sda.Update(newDT);
}
MessageBox.Show("Information Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}