SMO question

  • Thread starter Thread starter fniles
  • Start date Start date
F

fniles

I am using VB.NET 2005 and SQL Server 2005. Using the Server object from SQL
Server Management Object (SMO), how can I connect to a database on the
server ?
When I do it like the following, it gave me an error "Logon failure: unknown
user name or bad password" even though the user id and password is correct.

Dim Server As Server = New Server("myServer")
Server.ConnectionContext.ConnectAsUser = True
Server.ConnectionContext.ConnectAsUserName = "myUserID"
Server.ConnectionContext.ConnectAsUserPassword = "myPassword"
Dim db As Database = Server.Databases("myDB") -> Error here

Thank you.
 
It looks like you are trying to connect to a DB like an ODBC type
connection. How about using a sqlDataAdapter?

---------------------------------------------------
Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
Dim da As SqlDataAdapter, conn As SqlConnection, ds As Dataset
Private Sub Form1_Load(...) Handles...
conn = New SqlConnection
conn.ConnectionString = "Data Source=yourServer;Initial
Catalog=yourDB;Integrated Security=True"

da = New SqlDataAdapter
da.SelectCommand = New SqlCommand
da.SelectCommand.Connection = conn
da.SelectCommand.CommandText = "Select * From SomeTable"

ds = New DataSet
da.Fill(ds, "tbl1")

DataGridView1.DataSource = ds.Tables("tbl1")
End Sub

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

this sample is using MixedMode security on the sql server (Windows
authentication, actually). This way you don't have to include the
UserID and password in the connection string.

Rich
 
Back
Top