如何通过texbox和datagridview更新主键 [英] How to update primary key through texbox and datagridview

查看:89
本文介绍了如何通过texbox和datagridview更新主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有C#的Windows窗体,我有一个gridview和4个texboxes和更新按钮,我想通过texbox更新所选行,我能够更改名称,大小,日期,但我无法更改Id,我希望能够更改所有列。我怎么能这样做!!





I am using windows form with C#, I have a gridview and 4 texboxes and update button, I want to update the selected row through texboxes, I am able to change name, size, date but I can't change Id, I want to be able to change all the columns. How can I do that !!


private void Updatebtn_Click_1(object sender, EventArgs e)
       {

               SqlConnection conn = new SqlConnection(@"Data Source=Ali-pc\sqlexpress;Initial Catalog=DB;Integrated Security=True");
               SqlCommand cmd = new SqlCommand("update stock set Id=@Id, Name= @Name,  Size= @Size, Date=@Date where Id=@Id ", conn);
               conn.Open();
               cmd.Parameters.AddWithValue("@Id", textBox1.Text);
               cmd.Parameters.AddWithValue("@Name", textBox2.Text);
               cmd.Parameters.AddWithValue("@Size", textBox3.Text);
               cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Value);
               cmd.ExecuteNonQuery();
               MessageBox.Show("Successfully Changed ", "message", MessageBoxButtons.OK, MessageBoxIcon.Information);
               DataTable ds = new DataTable();
               SqlDataAdapter sda = new SqlDataAdapter("Select * from stock", conn);
               sda.Fill(ds);
               BindingSource bs = new BindingSource();
               bs.DataSource = ds;
               dataGridView1.DataSource = bs;

           }

推荐答案

请参阅PIEBALDconsult,ppolymorphe和我的问题的评论。他们应该解释你为什么你的问题毫无意义。此键是元数据的一部分,在您定义数据库模式的表,属性和其他组件时定义。



但我可以向您展示一种开发系统的方法,您可以更改所有列。我知道做什么好事,但你正在思考一个完全错误的方向。我会解释你,但你需要锻炼生动的想象力,把握它。



这是你可以做的:你可以改变列,但不能在你现在正在使用的数据库中,但是在表格中另一个数据库,您在数据库中建模的数据库。你明白了吗?您必须使用数据库来创建一个模式,该模式旨在为数据库中的一些更简单的数据库建模。换句话说,您可以为模式中的一些数据库概念建模。您可以在数据库中为属性,属性,键和表等概念建模。然后,您可以拥有属性,表,属性等表。比方说,您现在正在使用的数据库中执行此操作。我们称之为实施数据库。它的架构将模拟其中的另一个数据库;称之为模型数据库。 属性,属性等概念将是模型数据库的数据库元数据的概念。然后你需要根据元数据对其数据进行建模。



我将在一个简单的例子中解释它。假设您想要使用一些记录来实现该表,并且可以随时添加或重命名该表的列。为简单起见,我们假设所有键和所有数据元素都只是字符串。这是你可以做的。定义列名称或属性表以及值表。然后定义单元格表。每个单元格都是一个带有两个键的数据库记录:一个指向属性,一个指向值。这样,可以重用值。然后,模型数据库中的表将组成单元格表。这样,您可以拥有一组属性/值对(键/值,如果您愿意),它们可以表示为一个表(建模表,而不是您的实现数据库的表),可以理解为一个表列是属性,单元格是值。但实际上(在实现演示中),这只是属性和值之间的关系。更改属性名称后会发生什么?没什么不好;您的实现数据库的完整性仍将保留,列名称不是键,只是一组名称(字符串)。同样,您可以添加新的属性对象。它可以表示为未填充任何值的新列。当您设置指向现有值或新值的某些键时,将添加值。



您能理解这个想法吗?如果没有,那么对你来说最好的事情就是学习关系模型;你的问题表明你现在还不太了解它。无论如何,欢迎您提出后续问题。



-SA
Please see the comments to the question, by PIEBALDconsult, ppolymorphe and mine. They should explain you why your question makes no sense at all. This key is a part of metadata, which is defined when you define tables, attributes and other components of database schema.

But I can show you a way of you need to develop a system where you can "change all columns". I understand where it can be good to do, but you are thinking in a totally wrong direction. I'll explain you, but you well need to exercises vivid imagination, to grasp it.

Here is what you can do: you can "change the columns", but not in the database you are working right now, but in the tables of another database, the one you model inside your database. Are you getting the idea? You have to use your database to create a schema which is intended to model some simpler database inside your database. In other word, you model some of the database concepts in your schema. You can model the concepts such as "property", "attribute", "key" and "table" inside your database. Then you can have tables of properties, tables, attributes, and so on. Let's say, you are doing it in your database you are working with right now. Let's call it "implementation database". It's schema will model another database inside it; call it "model database". The concepts of "property", "attribute", etc. will be the concepts of the database metadata for your model database. Then you will need to model its data written according to the metadata.

I'll explain it on a simple example. Let's say, you want to implement the table with some records, and the columns of this table can be added or renamed at any time. For simplicity, let's assume that all keys and all data elements are just strings. Here is what you can do. Define table of "column names" or "properties", and table of "values". Then define the table of "cell". Each cell would be a database record with two keys: one pointing to a "property" and one to "value". This way, values can be reused. And then your table in the model database will be composed as a table of "cells". This way, you can have a set of "property/value" pairs (key/value, if you will) which can be represented as a table (modeled table, not table of your implementation database) which can be understood as a table which columns are properties and cells are values. But in fact (in implementation presentation), this is just a relation between "properties" and "values". What happens when you change the "property name"? Nothing bad; the integrity of your implementation database will still preserved, that "column names" are not keys, just a set of names (strings). Same way, you can add a new "property" object. It can be represented as a "new column" not filled with any values. Values will be added when you set some keys pointing to existing values or new values.

Can you grasp the idea? If not, it's possible that the best thing for you would be learning the relational model; your question indicates that you don't quite understand it at the moment. Anyway, your follow-up questions will be welcome.

—SA


这篇关于如何通过texbox和datagridview更新主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆