【问题标题】:Using SQL script through vb.net code通过 vb.net 代码使用 SQL 脚本
【发布时间】:2012-02-15 18:02:23
【问题描述】:

我是 SQL Server 的新手。我经常在 Internet 上找到脚本来使用 SQL Server 执行不同的功能,但我不知道如何在 vb.net 中使用它们。

例如,我想通过我的 vb.net 应用程序运行以下代码,但不知道该怎么做。请指教

ALTER LOGIN sa ENABLE ; 去 ALTER LOGIN sa WITH PASSWORD = '' ; 去吧

谢谢

【问题讨论】:

    标签: sql-server vb.net


    【解决方案1】:

    以下代码可能会有所帮助。来自http://www.daniweb.com/software-development/vbnet/code/216920

    'Declare outside of class
    Imports System.Data.SqlClient
    
    
    'Declare inside of class >
    Dim SQLStr As String
    Private ConnString As String
    
    'Connstring = Server Name, Database Name, Windows Authentication 
    connstring = "Data Source=myserver;Initial Catalog=databasename;Integrated Security=True"
    
    'SQL Staments
    
    'SQL query = myQuery = "SQL Statment"
    
    SQLStr = "SELECT * FROM tblQuestion"
    
    SQLStr = "INSERT into tblQuestion(Name, Question) VALUES('Fred', 'How to use SQL?')"
    
    SQLStr = "UPDATE tblQuestion SET Answer = 'Like this' Where Question = 'How to use SQL?'"
    
    SQLStr = "DELETE FROM tblQuestion WHERE Question='How to use SQL?'"
    
    'Write to SQL
    
    Dim SQLConn As New SqlConnection() 'The SQL Connection
    Dim SQLCmd As New SqlCommand() 'The SQL Command
    
    SQLConn.ConnectionString = ConnString 'Set the Connection String
    SQLConn.Open 'Open the connection
    
    SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
    SQLCmd.CommandText = SQLStr 'Sets the SQL String
    SQLCmd.ExecuteNonQuery() 'Executes SQL Commands Non-Querys only
    
    SQLConn.Close() 'Close the connection  
    
    
    
    
    
    
    
    
    
    'Read from SQL
    
    Dim SQLConn As New SqlConnection() 'The SQL Connection
    Dim SQLCmd As New SqlCommand() 'The SQL Command
    Dim SQLdr As SqlDataReader        'The Local Data Store
    
    SQLConn.ConnectionString = ConnString 'Set the Connection String
    SQLConn.Open 'Open the connection
    
    SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
    SQLCmd.CommandText = SQLStr 'Sets the SQL String
    SQLdr = SQLCmd.ExecuteReader 'Gets Data
    
    While dr.Read() 'While Data is Present        
          MsgBox(dr("Column Name")) 'Show data in a Message Box
    End While
    
    Loop While SQLdr.NextResult() 'Move to the Next Record
    SQLdr.Close 'Close the SQLDataReader        
    
    SQLConn.Close() 'Close the connection
    

    【讨论】:

    • SQLClient 库的MSDN documentation 在我试图弄清楚这些东西是如何工作的时候提供了难以置信的帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多