简体   繁体   English

动态 SQL 问题 - C# Forms (Datagridview)

[英]Dynamic SQL issue - C# Forms (Datagridview)

I am currently trying to update my DataGridView to my database.我目前正在尝试将我的 DataGridView 更新到我的数据库。 I want to be able to update it with the enter key.我希望能够使用回车键更新它。 But I am getting this error:但我收到此错误:

"Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information." “不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成。”

Here is a picture of the code(as well its below in code snippet) Some of the code这是代码的图片(以及下面的代码片段)一些代码

Here is how the Datagridview is loaded:以下是 Datagridview 的加载方式:

        private void RampBoardLoader()
    {
        SqlConnection connection = new SqlConnection(ConnectionLoader.ConnectionString("Threshold"));
        connection.Open();
        SqlCommand selectRampBoard = new SqlCommand("Select_Ramp_Data", connection);
        selectRampBoard.CommandType = CommandType.StoredProcedure;
        selectRampBoard.Parameters.AddWithValue("@DateID", dateTimeRamp.Value.Date);
        dt = new DataTable();
        dataAdapter = new SqlDataAdapter(selectRampBoard);
        dset = new DataSet();
        dataAdapter.Fill(dset, "Ramp_Board");
        dgvRampBoard.DataSource = dset.Tables[0];
        connection.Close();

    }

Here is where I'm trying to update the datagridview:这是我尝试更新数据网格视图的地方:

        private void dgvRampBoard_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            e.Handled = true;

            scb = new SqlCommandBuilder(dataAdapter);
            dgvRampBoard.EndEdit();
            this.dataAdapter.Update(dset, "Ramp_Board");

        }

    }

I've been looking around to try to find the answer to this.我一直在四处寻找,试图找到这个问题的答案。 Every post I see everyone says that you need to make sure your table has a primary key.我看到每个人的每一篇帖子都说你需要确保你的表有一个主键。 My table does have a primary key.我的表确实有一个主键。 It has 2 primary keys.它有 2 个主键。 "Flight Number" and "DateID". “航班号”和“日期 ID”。 I'm not sure if it's because I have 2 primary keys, if that's why I'm getting this issue.我不确定是不是因为我有 2 个主键,如果这就是我遇到此问题的原因。

Attached is image of Database of my stored procedure.附件是我的存储过程的数据库图像。 You can also see the columns, I have 2 primary keys Stored Procedure您还可以看到列,我有 2 个主键存储过程

How to make life easy:如何让生活变得轻松:

(Note; this only works in a.net framework project; the designer has bugs in.net core and Microsoft have disabled it. There are tricks you can pull if you want to work in core, but they basically entail using a.net framework project to do the editing and a.netcore to do the running) (注意;这只适用于 .net 框架项目;设计者在 .net 核心中存在错误,微软已将其禁用。如果您想在核心中工作,可以使用一些技巧,但它们基本上需要使用 .net 框架项目进行编辑和 a.netcore 进行运行)

  • add a dataset type file to your project将数据集类型文件添加到您的项目
  • open it, right click its surface, choose add tableadapter打开它,右键单击它的表面,选择添加表适配器
  • Set up your connection设置您的连接
  • Choose to use stored procedures to get your data选择使用存储过程来获取数据
  • Choose your sprocs (I assume you have one for updating?)选择你的存储过程(我假设你有一个用于更新?)
  • Name the Fill/GetData methods appropriately (FillByDateId?)适当命名 Fill/GetData 方法(FillByDateId?)
  • Save the dataset保存数据集
  • Go to a new form Go 转新表
  • Open DataSources window on View menu, other windows在视图菜单上打开数据源 window,其他 windows
  • Drag the node representing your table, onto your form.将代表您的表格的节点拖到您的表单上。 You should see a grid, binding source, dataset and navigator appear您应该会看到网格、绑定源、数据集和导航器出现

That's it, you should be able to just run the app now, enter some date Id in the box, hit fill, change the data, hit save..就是这样,您现在应该可以运行该应用程序,在框中输入一些日期 ID,点击填充,更改数据,点击保存。

You can find the code the designer write for you in the normal code behind and in the FormXx.Designer.cs files if you're curious how it works.如果您好奇它是如何工作的,您可以在后面的正常代码和 FormXx.Designer.cs 文件中找到设计师为您编写的代码。 A tableadaptermanager will call tableadapter.Update(datatable) on "parent, child" order for every table that needs updating. tableadaptermanager 将为每个需要更新的表调用tableadapter.Update(datatable)在“父,子”顺序上。 Table adapter's Update uses the InsertCommand, UpdateCommand and DeleteCommand as appropriate to persist each row change according to what the row state is表适配器的 Update 根据行 state 的内容适当地使用 InsertCommand、UpdateCommand 和 DeleteCommand 来持久化每一行更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM