首页 > 解决方案 > 如何在sql表的新多列表中插入特定列

问题描述

我怎样才能做到这一点?当我在 sql 表中有两列以上时,是否可以只在两列上插入请有人帮我实现这一点。

我在下面尝试了这段代码:

 SqlConnection Conn = new SqlConnection(shoolmanangmentconn);
                         Conn.Open();
 SqlCommand cmd = new SqlCommand("INSERT INTO tbl_TestingThatSubjects(INDO, LANGUAGE,BIOLOGY, 
 GEOGRAPHY) VALUES (@IDNO,@LANGUAGE,@BIOLOGY,@GEOGRAPHY)", Conn);
                         cmd.Parameters.AddWithValue("@IDNO", txtids.Text); 
                         cmd.Parameters.AddWithValue("@LANGUAGE", txtlanguage.Text);    
                         cmd.ExecuteNonQuery();

我试图只插入 LANGUAGE 列而不插入其他列请有人教我如何做正确的事情。(我正在使用 wpf c#)

标签: c#

解决方案


The columns either need to Allow Nulls or have a Default Value:

enter image description here

This is how you can extend database schema's without breaking existing codebases.

REF: https://blog.sqlauthority.com/2017/07/02/default-value-nullable-column-works-interview-question-week-129/

ps look into the using statement for unmanaged objects, such as the SqlConnection and SqlCommand objects.


推荐阅读