Avatar billede loukas Mester
09. januar 2004 - 15:55 Der er 3 kommentarer og
1 løsning

mssql connection vil ikke åbne ?

Jeg er ved at gennemgå bogen: Beginning VB.net 2nd edition.
Chapter 16 - side 638.
Jeg får en fejl på myConnection.Open()
måske er det et mssql spørgsmål !!!
Med Enterprise manager kan jeg se under 'Users' at der en bruger
Name - dbo
Login - sa



Koden til jer der ikke har bogen:
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
    Inherits System.Windows.Forms.Form
    ' Declare objects...
    Dim myConnection As SqlConnection = New _
        SqlConnection("server=(localhost);database=pubs;uid=sa;pwd=")

    Dim myDataAdapter As New SqlDataAdapter()
    Dim myDataSet As DataSet = New DataSet()

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    Friend WithEvents grdAuthorTitles As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.grdAuthorTitles = New System.Windows.Forms.DataGrid()
        CType(Me.grdAuthorTitles, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'grdAuthorTitles
        '
        Me.grdAuthorTitles.DataMember = ""
        Me.grdAuthorTitles.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.grdAuthorTitles.Location = New System.Drawing.Point(16, 16)
        Me.grdAuthorTitles.Name = "grdAuthorTitles"
        Me.grdAuthorTitles.Size = New System.Drawing.Size(528, 296)
        Me.grdAuthorTitles.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(552, 325)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.grdAuthorTitles})
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.grdAuthorTitles, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Set the SelectCommand properties...
        myDataAdapter.SelectCommand = New SqlCommand()
        myDataAdapter.SelectCommand.Connection = myConnection
        myDataAdapter.SelectCommand.CommandText = _
            "SELECT au_lname, au_fname, title, price " & _
            "FROM authors " & _
            "JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
            "JOIN titles ON titleauthor.title_id = titles.title_id " & _
            "ORDER BY au_lname, au_fname"
        myDataAdapter.SelectCommand.CommandType = CommandType.Text

        ' Open the database connection...
        myConnection.Open()
        ' Now execute the command...
        myDataAdapter.SelectCommand.ExecuteNonQuery()

        ' Fill the DataSet object with data...
        myDataAdapter.Fill(myDataSet, "authors")

        ' Close the database connection...
        myConnection.Close()

        ' Set the DataGrid properties to bind it to our data...
        grdAuthorTitles.DataSource = myDataSet
        grdAuthorTitles.DataMember = "authors"

        ' Declare objects for the DataGrid...
        Dim objDataGridTableStyle As New DataGridTableStyle()
        Dim objTextCol As New DataGridTextBoxColumn()

        ' Set the AlternatingBackColor property...
        objDataGridTableStyle.AlternatingBackColor = Color.WhiteSmoke

        ' Set the MappingName for the DataGridTableStyle...
        objDataGridTableStyle.MappingName = "authors"

        ' Set the MappingName for the first column...
        objTextCol.MappingName = "au_lname"
        ' Set the new HeaderText...
        objTextCol.HeaderText = "Last Name"
        ' Add the column to the DataGridTableStyle...
        objDataGridTableStyle.GridColumnStyles.Add(objTextCol)

        ' Get a new reference to the DataGridTextBoxColumn...
        objTextCol = New DataGridTextBoxColumn()
        ' Set the MappingName for the second column...
        objTextCol.MappingName = "au_fname"
        ' Set the new HeaderText...
        objTextCol.HeaderText = "First Name"
        ' Add the column to the DataGridTableStyle...
        objDataGridTableStyle.GridColumnStyles.Add(objTextCol)

        ' Get a new reference to the DataGridTextBoxColumn...
        objTextCol = New DataGridTextBoxColumn()
        ' Set the MappingName for the third column...
        objTextCol.MappingName = "title"
        ' Set the new HeaderText...
        objTextCol.HeaderText = "Book Title"
        ' Set the Width of the column...
        objTextCol.Width = 304
        ' Add the column to the DataGridTableStyle...
        objDataGridTableStyle.GridColumnStyles.Add(objTextCol)

        ' Get a new reference to the DataGridTextBoxColumn...
        objTextCol = New DataGridTextBoxColumn()
        ' Set the MappingName for the fourth column...
        objTextCol.MappingName = "price"
        ' Set the new HeaderText...
        objTextCol.HeaderText = "Retail Price"
        ' Set the Alignment within the column...
        objTextCol.Alignment = HorizontalAlignment.Right
        ' Add the column to the DataGridTableStyle...
        objDataGridTableStyle.GridColumnStyles.Add(objTextCol)

        ' Add the DataGridTableStyle to the DataGrid...
        grdAuthorTitles.TableStyles.Add(objDataGridTableStyle)

    End Sub
End Class
Avatar billede arne_v Ekspert
09. januar 2004 - 16:01 #1
Gæt:

din SQLServer er sat op til NT only security og ikke mixed mode
security (sa er en SQLServer security ting)

Prøv med:

Dim myConnection As SqlConnection = New _
        SqlConnection("server=(localhost);database=pubs;Integrated Security=SSPI")
Avatar billede loukas Mester
09. januar 2004 - 17:28 #2
Jeg har prøvet med foreslaget, deværre.
Hvordan sætter jeg SQL-Serveren op til mixed mode ?
Avatar billede arne_v Ekspert
09. januar 2004 - 18:19 #3
Bare lav KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\xxxxxxx\MSSQLServer\LoginMode
om fra 1 til 2 og genstart.

Men hvis ikke ændringe hjalp, så hjælper det ikke.


Hvilken fejl får du ?
Avatar billede mikl-dk Nybegynder
31. oktober 2004 - 17:18 #4
Fjern () omkring localhost, så der står:
Dim myConnection As SqlConnection = New SqlConnection("server=localhost;database=pubs;uid=sa;pwd=")

Så virker det - med garanti :)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester