vb.net 使用 ADO.NET 命令向 VB 中的 SQL 表添加一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14971973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:25:14  来源:igfitidea点击:

adding a column to a SQL table in VB using ADO.NET commands

sqlsql-servervb.netado.net

提问by morgred

I am interested in a minimal set of instructions that allow me to add a column into an existing table in visual basic using ado.net components. My database is made in sql server. I would greatly appreciate a practical commentary to the code as that works best for me

我对一组最少的指令感兴趣,这些指令允许我使用 ado.net 组件将一列添加到 Visual Basic 中的现有表中。我的数据库是在 sql server 中创建的。我非常感谢对代码的实用评论,因为它最适合我

edit 1

编辑 1

Imports System.Data.Sql
Imports System.Data.SqlClient

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim connString As String = "Data Source=THE_SHOGUNATE\SQLEXPRESS;Initial Catalog=le_database;Integrated Security=True"
        Dim conn As New SqlConnection(connString)
        conn.Open()
        Dim comm As New SqlCommand("SELECT denumire FROM Reparatii", conn)
        Dim reader As SqlDataReader = comm.ExecuteReader
        Dim dt As New DataTable
        dt.Load(reader)
        ListBox1.DataSource = dt
        ListBox1.DisplayMember = "denumire"
        conn.Close()

        conn.Open()
        Dim comm As New SqlCommand("ALTER TABLE reparatii ADD durata_executie INT", conn)

        conn.Close()
    End Sub
End Class

Here's a set of instructions meant for testing purposes and some wishful coding

这是一组用于测试目的的说明和一些一厢情愿的编码

回答by Mitch Wheat

conn.Open()
Dim comm As New SqlCommand("ALTER TABLE reparatii ADD durata_executie INT", conn)

comm.ExecuteNonQuery()

I would also suggest using a usingstatement to automatically dispose of objects.

我还建议使用using语句来自动处理对象。