【发布时间】:2020-09-28 04:34:38
【问题描述】:
当我更改此 DataGridView 中的单元格并单击我制作的“更新”按钮时,我试图让我的“笑话”表中的数据在 SQL Server 中更新。我尝试按照教程进行操作,但似乎不起作用。我需要在这里更改什么才能使其正常工作?我没有奇怪地收到任何错误,所以它一定是我做的方式或其他东西。感谢您的帮助。
这是完整的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace TestProject1
{
public partial class UserControl3 : UserControl
{
SqlConnection con = new SqlConnection(@"Data Source=MSI;Initial Catalog=Jokes;Integrated Security=True;");
SqlDataAdapter adpt;
DataTable dt;
DataSet ds;
SqlCommandBuilder cmdbl;
public UserControl3()
{
InitializeComponent();
ShowData();
}
private void fillByToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.jokesTableAdapter.FillBy(this.jokesDataSet.Jokes);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
private void UserControl3_Load(object sender, EventArgs e)
{
con.Open();
ds = new DataSet();
adpt.Fill(ds, "Jokes");
}
public void ShowData()
{
adpt = new SqlDataAdapter("SELECT * FROM Jokes", con);
dt = new DataTable();
adpt.Fill(dt);
dataGridView1.DataSource = dt;
}
private void BackGridButton_Click(object sender, EventArgs e)
{
this.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
cmdbl = new SqlCommandBuilder(adpt);
adpt.Update(ds, "Jokes");
MessageBox.Show("Jokes Updated");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
【问题讨论】:
标签: c# sql-server visual-studio winforms