【问题标题】:Inserting Data Into SQL Server Database Through DataGridView Not Working In Visual Studio通过 DataGridView 将数据插入 SQL Server 数据库在 Visual Studio 中不起作用
【发布时间】: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


    【解决方案1】:

    仔细检查(通过调试)属性builder.GetUpdateCommand().CommandText 是否返回有效的更新命令。

    如果没有,请在 button1_Click 中添加更新您的 try-block 以使其看起来像这样

    cmdbl = new SqlCommandBuilder(adpt);
    adpt.UpdateCommand = cmdbl.GetUpdateCommand();
    adpt.Update(ds, "Jokes");
    MessageBox.Show("Jokes Updated");
    

    我确实承认,从理论上看,从您实例化 SqlCommandbuilder 的方式来看,不应该要求上面的第二行代码 sn-p。但这只是再次确认,也是我建议在调试模式下检查该属性的部分原因。

    【讨论】:

    • 现在它说,“名称'适配器'在当前上下文中不存在”,以及,“名称'sqlBld'在当前上下文中不存在”。是不是拼写错误?
    • 哦,是的,我只是添加了示例行,没有注意匹配变量名,我的错。现在编辑了。
    • 奇数。当我尝试单击更新按钮时出现此错误:“不支持不返回任何键列信息的 SelectCommand 为 UpdateCommand 生成动态 SQL。”
    • 哦,没关系,我修好了。我不得不进入 SSMS 并添加一个主键。我做了主键“JokeID”。不幸的是,现在我仍然有主要问题。每当我在更改 DataGridView 中的一个单元格后单击“更新”按钮时,它会显示“笑话更新”,但它不会在数据库中更改它。当我停止并重新启动表单,或者在 SQL Server 中执行 SELECT * FROM Jokes 查询时,它根本没有改变。
    • 你在调试中检查过这个值吗 - cmdbl.GetUpdateCommand().CommandText
    猜你喜欢
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    相关资源
    最近更新 更多