在 VB 2010 的数据表中插入新列时,如何更新 SQL Server 2008 数据库? [英] When inserting new columns in a datatable in VB 2010, how do I update the SQL Server 2008 Database?

查看:16
本文介绍了在 VB 2010 的数据表中插入新列时,如何更新 SQL Server 2008 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 VB 2010 和 SQL Server 2008:

Using VB 2010 and SQL Server 2008:

我能够在数据表中插入新行、删除行和更新数据,并使用数据适配器更新数据库,没有任何问题.当我尝试添加新列时,该列出现在数据表中,但不会更新到数据库中.我目前正在使用 DataAdapter.Update(DataTable) 方法.

I am able to insert new rows, delete rows, and update data in a datatable, and use a data adapter to update the database without any issues. When I try to add new columns, the column appears in the data table, but it does not update to the database. I currently am using the DataAdapter.Update(DataTable) Method.

我的代码如下:

Dim dcNewColumn As New DataColumn
dcNewColumn.ColumnName = strClassColumnName
m_DataTable.Columns.Add(dcNewColumn)
m_DataTable.Row(intCurrentRow).Item(strClassColumnName) = strClassName
dcNewColumn = Nothing

m_DA.Update(m_DataTable)

插入新列时是否有不同的方法来更新数据库?

Is there a different method to updating the database when inserting new columns?

推荐答案

您正在寻找 ALTER TABLE sql 命令.
或者 SQLDMO 列集合(请检查页面开头的通知)

You are looking for the ALTER TABLE sql command.
Or the SQLDMO Columns Collection (Please check the notice at the start of the page)

添加列的 ALTER TABLE 命令可以用这种方式编码

An ALTER TABLE Command to add a column could be coded in this way

Dim cn As SqlConnection
Dim cmd As SqlCommand
Using cn = GetConnection()
    cn.Open()
    Using cmd = new SqlCommand()
         cmd.Connection = cn
         cmd.CommandText = "ALTER TABLE myTable ADD aNewColumn VARCHAR(20) NULL"
         cmd.CommandType = CommandType.Text
         cmd.ExecuteNonQuery()
    End Using
End Using

这篇关于在 VB 2010 的数据表中插入新列时,如何更新 SQL Server 2008 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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