2012-02-15 46 views
1

我是SQL Server的新手。我經常通過互聯網找到腳本來執行與SQL Server不同的功能,但我不知道如何在vb.net中使用它們。通過vb.net使用SQL腳本代碼

例如,我想通過我的vb.net應用程序運行以下代碼,但不知道如何去做。請告知

ALTER LOGIN sa ENABLE; GO ALTER LOGIN sa WITH PASSWORD =''; GO

感謝

回答

2

下面的代碼可能會有所幫助。它從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 
+1

的[MSDN文檔(http://msdn.microsoft.com/en-us/library/8t72t3k4.aspx)爲SQLCLIENT圖書館是當我試圖找出一個令人難以置信的幫助這些東西如何工作。 – lyrisey 2012-02-15 18:27:41