stillwillyboy
Technical User
I am trying to “convert” some code in Sam’s Visual Basic 2005 Express book. I am trying to connect to a SQL 2005 Express db. The code in Sam’s is for connecting to Jet. I’ve looked at some of the other connection Q’s and replies on this site, but being a newbie, well….. Here is my code with notes:
Public Class frmConnectToSQLTest
Private m_cnADONetConnection As New System.Data.SqlClient.SqlConnection()
Private m_daDataAdapter As SqlClient.SqlDataAdapter
Private m_cbCommandBuilder As SqlClient.SqlCommandBuilder
Private m_dtClients As New DataTable
Private m_rowPosition As Integer = 0
Private Sub frmConnectToSQLTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
m_cnADONetConnection.ConnectionString = _
"Provider=Microsoft.SQL.2005; data source= C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\ClientPayroll.mdf"
'In the line above with “Provider=Microsoft.SQL.2005;” included I Receive the error message telling me that the keyword “Provider” is not supported. When I leave it out, I get past the above code, but then the following line:
m_cnADONetConnection.Open()
‘trips up with the message: “An error has occurred while ‘establishing a connection to the server. When connecting to SQL Server 2005, this failure may be ‘caused by the fact that under the default settings SQL Server does not allow remote connections.”
‘How do I fix this?
‘Code continues:
m_daDataAdapter = New SqlClient.SqlDataAdapter("Select * from dbo.tbl_ClntBasicInfo", m_cnADONetConnection)
m_cbCommandBuilder = New SqlClient.SqlCommandBuilder(m_daDataAdapter)
m_daDataAdapter.Fill(m_dtClients)
Me.ShowCurrentRecord()
End Sub
Private Sub frmConnectToSQLTest_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
m_cnADONetConnection.Close()
m_cnADONetConnection.Dispose()
End Sub
Private Sub ShowCurrentRecord()
If m_dtClients.Rows.Count = 0 Then
txtClientNumber.text = ""
txtLastName.text = ""
txtFirstName.text = ""
Exit Sub
End If
txtClientNumber.text = _
m_dtClients.Rows(m_rowPosition)("ClientNumber").ToString
txtLastName.text = _
m_dtClients.Rows(m_rowPosition)("LastName").ToString
txtFirstName.text = _
m_dtClients.Rows(m_rowPosition)("FirstName").ToString
End Sub
End Class
My server type is Database Engine.
My server name is: B-7M2KXC45F9FT4\SQLEXPRESS
I connect to my DB thru the SQL Server Management Express.
I use Windows Authority.
I believe the SQL Server version is “SQL Server 9.0.1399”
My DB name is “ClientPayroll”
The table name in ClientPayroll is “dbo.tbl_ClntBasicInfo”
After I establish the connection, I then want to be able to enter a number in the Client Number field and have the client’s information (address, phone, etc.) appear on the form.
If you see any other issues, please let me know.
TIA,
Bill
Public Class frmConnectToSQLTest
Private m_cnADONetConnection As New System.Data.SqlClient.SqlConnection()
Private m_daDataAdapter As SqlClient.SqlDataAdapter
Private m_cbCommandBuilder As SqlClient.SqlCommandBuilder
Private m_dtClients As New DataTable
Private m_rowPosition As Integer = 0
Private Sub frmConnectToSQLTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
m_cnADONetConnection.ConnectionString = _
"Provider=Microsoft.SQL.2005; data source= C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\ClientPayroll.mdf"
'In the line above with “Provider=Microsoft.SQL.2005;” included I Receive the error message telling me that the keyword “Provider” is not supported. When I leave it out, I get past the above code, but then the following line:
m_cnADONetConnection.Open()
‘trips up with the message: “An error has occurred while ‘establishing a connection to the server. When connecting to SQL Server 2005, this failure may be ‘caused by the fact that under the default settings SQL Server does not allow remote connections.”
‘How do I fix this?
‘Code continues:
m_daDataAdapter = New SqlClient.SqlDataAdapter("Select * from dbo.tbl_ClntBasicInfo", m_cnADONetConnection)
m_cbCommandBuilder = New SqlClient.SqlCommandBuilder(m_daDataAdapter)
m_daDataAdapter.Fill(m_dtClients)
Me.ShowCurrentRecord()
End Sub
Private Sub frmConnectToSQLTest_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
m_cnADONetConnection.Close()
m_cnADONetConnection.Dispose()
End Sub
Private Sub ShowCurrentRecord()
If m_dtClients.Rows.Count = 0 Then
txtClientNumber.text = ""
txtLastName.text = ""
txtFirstName.text = ""
Exit Sub
End If
txtClientNumber.text = _
m_dtClients.Rows(m_rowPosition)("ClientNumber").ToString
txtLastName.text = _
m_dtClients.Rows(m_rowPosition)("LastName").ToString
txtFirstName.text = _
m_dtClients.Rows(m_rowPosition)("FirstName").ToString
End Sub
End Class
My server type is Database Engine.
My server name is: B-7M2KXC45F9FT4\SQLEXPRESS
I connect to my DB thru the SQL Server Management Express.
I use Windows Authority.
I believe the SQL Server version is “SQL Server 9.0.1399”
My DB name is “ClientPayroll”
The table name in ClientPayroll is “dbo.tbl_ClntBasicInfo”
After I establish the connection, I then want to be able to enter a number in the Client Number field and have the client’s information (address, phone, etc.) appear on the form.
If you see any other issues, please let me know.
TIA,
Bill