转换新列中的列中的行访问C# [英] Convert rows in a column in new colums access C#

查看:89
本文介绍了转换新列中的列中的行访问C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这样的事情:

我有一个表格名称容器默认有2个字符串



专栏[key] Column2

| ------------- | silos1 |

| ------------- | silos2 |

| ------------- | silos3 |

| ------------- | silos4 |

| ------------- | silos5 |





我想要点击按钮添加第2列中的行的新闻列并检查列是否已存在。

列[关键] Column2 silos1 silos2 silos3 silos4 silos5

| ------------- | silos1 |

| ------------- | silos2 |

| ------------- | silos3 |

| ------------- | silos4 |

| ------------- | silos5 |



到目前为止我要添加新闻专栏:







Hi,I'm trying do something like this:
I have a table name Container that have 2 colums by default

Column[key] Column2
|-------------| silos1|
|-------------| silos2|
|-------------| silos3|
|-------------| silos4|
|-------------| silos5|


I want when a click a button to add news columns foreach rows in Column 2 and check if the columns already exist.
Column[key] Column2 silos1 silos2 silos3 silos4 silos5
|-------------| silos1|
|-------------| silos2|
|-------------| silos3|
|-------------| silos4|
|-------------| silos5|

Until now to add news colums i use :



OleDbCommand cmd = new OleDbCommand("ALTER TABLE Container ADD COLUMN silos1 NUMBER", con);
cmd.ExecuteNonQuery();





我正在使用数据表来填充数据网格



I'm using a datatable to fill a datagridview

sda = new OleDbDataAdapter("Select * From Container  ", con);
            OleDbCommandBuilder cb = new OleDbCommandBuilder(sda);
           



            dt = new DataTable();
            sda.Fill(dt);

            dataGridView1.DataSource = dt;





我尝试过: < br $> b $ b

----------------------------------- -----------------------------



What I have tried:

----------------------------------------------------------------

推荐答案

Hi Arthuro



不想让我的ui个人改变我的数据库结构,但那不是问题,你的alter语句出错了,数据类型号是一个olap类型。在SQL中你可以选择int或者类似的东西:

数据类型(Transact-SQL)| Microsoft Docs [ ^ ]



然后您应该考虑是否可以将字段置为无效,当动态添加到具有数据的表时更多或者不太强制。如果您不希望它,您将使用有效值更新表格并更改它不允许。



现在我们得到



来自

Hi Arthuro

Wouldn't want to let my ui alter my database structure personally but then that's not the question, your alter statement appears wrong, the data type number is an olap type. In SQL you could choose int typically or numeric or something like that:
Data types (Transact-SQL) | Microsoft Docs[^]

then you should consider if the field can be nulled which when adding dynamically to a table with data is more or less mandatory. If you don't want it to be, you'll update the table with valid values afterwards and alter it not not allow.

Now we get

From
OleDbCommand cmd = new OleDbCommand("ALTER TABLE Container ADD COLUMN silos1 NUMBER", con);
cmd.ExecuteNonQuery();






To

OleDbCommand cmd = new OleDbCommand("ALTER TABLE Container ADD COLUMN silos1 int null", con);
cmd.ExecuteNonQuery();



在OleDbCommand上不太确定,我个人总是使用System.Data.SqlClient命名空间和SqlCommand



哦,你正在使用MS Access,带来回忆。在这种情况下,更改是使用NUMERIC,你将获得






Not too certain on the OleDbCommand, i personally always use the System.Data.SqlClient namespace and the SqlCommand

oh you're using MS Access, that brings memories. In that case the change is to use NUMERIC and you'll get

To

OleDbCommand cmd = new OleDbCommand("ALTER TABLE Container ADD COLUMN silos1 numeric", con);
cmd.ExecuteNonQuery();



你不必考虑该字段是否可以被取消



然后动态添加它们我建议使用你正在填充的数据表,因为它非常方便




And you won't have to consider if the field can be nulled

Then to add them dynamically i'd suggest using the datatable you're filling anyway as it's rather handy

var dt = new DataTable();
            sda.Fill(dt);
            var nameOfList = new List<string>(dt.Columns.Count);
            foreach (DataColumn clmn in dt.Columns)
            {
                nameOfList.Add(clmn.Caption);
            }

            var toAddList = new List<string>();

            foreach(DataRow row in dt.Rows)
            {
                if (!nameOfList.Contains(row["Column[key]"]))
                {
                    toAddList.Add(row["Column[key]"].ToString());
                }
            }

            foreach(string nameOfKey in toAddList)
            {
                //TODO: Execute add column
            }


这篇关于转换新列中的列中的行访问C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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