SQL Server connection failing in VB.net - sql-server

I want to connect to an SQL Server database. I am not experienced.
This part is a gray:
Imports System.Data.SqlClient
Public Class Conec
Dim cnx As New sql << here there is no SqlConnection
End Class

Related

Failing to connect remotely to sqlserver in vb.net

I am facing a challenge to connect remotely to sqlserver in vb.net. I am using MSSql Server 2014 Express Edition, The Error Message is as follows:
Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement. This could be because the pre-login handshake failed or the server was unable to respond back in time. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=184; handshake=14923;
My Vb.Net code is as follows:
Imports System.Data.SqlClient
Public Class Form1
Dim connection As New SqlConnection("Data Source=77.246.xx.xx,1433;Network Library=DBMSSOCN;Initial Catalog=company_info;User ID=CSPOS;Password=password1;")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
connection.Open()
TextBox1.Text = "You have connected to sql server successfully!!"
TextBox1.ForeColor = Color.Green
connection.Close()
Catch ex As Exception
TextBox1.Text = ex.Message
TextBox1.ForeColor = Color.Red
End Try
End Sub
End Class
I am stuck, I do not know how to handle it and make things work.
Update your connection string to
Data Source=77.246.xx.xx,1433;Database=company_info;User ID=CSPOS;Password=password1;Persist Security Info=True
It is works for me in VB.NET

Run time error -2147467259(80004005) for using ADODB.Connection

I'm trying to connect VBA to sql server f. I'm using Windows server 2016 data center and sql sever management studio V17.3 . So I used following objects in my VBA reference
While I'm trying to execute below line
Dim Cn As ADODB.Connection
I'm getting error message
Run time error -2147467259(80004005)
Automation error,Unspecified error
Can you guide me to resolve this error
Your ADO version is old (ADO 2.8 was included in Windows XP and Windows Server 2003). The next version is ADO 6 and "Microsoft ActiveX Data Objects 6.1 Library" is the newest.
It should be installed already on your computer, so just scroll down and mark that reference instead:
Try this
Sub GetData()
Dim cnDump As ADODB.Connection
Set cnDump = New ADODB.Connection
' Provide the connection string.
Dim strConn As String
'Use the SQL Server OLE DB Provider.
strConn = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=XXXX;Data Source=XXXX\XXXX;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=XXXX;Use Encryption for Data=False;Tag with column collation when possible=False;"
'Now open the connection.
cnDump.Open strConn

Visual Studio - SQLConnection not working on an SQL Server Express database

I'm trying to connect to an SQL Express database I have created as a .mdf file. The database is named PruebaDB.mdf and appears in the Data Connections section of the Server Explorer window:
https://imgur.com/tZvFtqn
It's data source is set to "Microsoft SQL Server Database File (SqlClient)"
This is my code:
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim connectionString As String = GetConnectionString()
Using connection As New SqlConnection(connectionString)
connection.Open()
'do other things after open works
End Using
End Sub
Private Function GetConnectionString() As String
Return "Data Source=.\SQLEXPRESS;AttachDbFileName=C:\Users\Daniel\Desktop\Apuestas\Apuestas\PruebaDB.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"
End Function
End Class
When I click the button, however, I get an exception (translated from Spanish):
Error related to the network or specific to the instance while establishing connection to the SQL Server server. Server was not found or is not accessible. Check that the instance name is correct and SQL Server is configured to admit remote connections.
What would be the problem? How can I connect to my database?
If I'm doing something wrong, how can I do a simple local database in Visual Studio (basic) for my project?

connection from access vba to odbc table

I use the code below to connect to a sql server table
Dim Cn1 As New ADODB.Connection
Dim Rs1 As New ADODB.Recordset
Cn1.Open "ODBC;DRIVER=SQL Server;DATABASE=\\\s100nlex\DATA\SQL Database.mdf", "Erik", "Passwd"
Rs1.Open "SELECT [ODBC Table].* FROM [ODBC Table];", Cn1
I can open the ODBC Table manually but get an error on the Cn1.open.... What could be wrong?
You don't specify the mdf file for SQL Server connections, you need to specify a server and a database:
Cn1.Open "ODBC;DRIVER=SQL Server;SERVER=s100nlex;DATABASE=DatabaseName", "Erik", "Passwd"

what's wrong with this ADODB connection string to sql server 2005

I'm working on populating a combobox in visual studio 2005 using vb and I'm stuck on the connection string part. I should mention i'm connecting to a SQL Server 2005 instance. Here is what i have:
Dim gDBA As ADODB.Connection
Dim records As ADODB.Recordset
gDBA = New ADODB.Connection
gDBA.Open("Server=e-13;Database=subscribers;User ID=KViews;Password=Solution;Trusted_Connection=False;", "KViews", "Solution")
I got the connection string from http://www.connectionstrings.com/sql-server-2005#p1
When I click 'run', i get a COMException was unhandled message : "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified."
I'm guessing the connection string is looking for a System ODBC driver name, however, i'm not sure where to put this. any ideas on this or what else I might be doing wrong?
thanks in advance
You're probably missing "Provider=SQLNCLI" or "Provider=SQLOLEDB" or "Driver={SQL Native Client}" from the conn string. The article you quoted shows 2 of these, but without SQL Native client installed you can rely on SQLOLEDB
This specifies the driver, otherwise it it derived from a DSN set via control panel. This explains the error.
This connection string is currently in use on an asp app connecting to Sql Server 2008 Express.
"Driver={SQL Native Client};Server=serverName;Database=databaseName;Uid=userId;pwd=password;connect timeout=60;"
What about using "Data Source" and "Initial Catalog" instead of "Server" and "Database"?
If you're connecting to a SQL server try using the System.Data.SqlClient namespace.
Dim cn As New SqlClient.SqlConnection("User ID=KViews;Password=Solution;Initial Catalog=subscribers;Data Source=e-13")
cn.Open()
Dim cmd As New SqlClient.SqlCommand("Select * from tabel")
cmd.Connection = cn
Dim r As SqlClient.SqlDataReader = cmd.ExecuteReader
(SqlClient is managed code - ADODB native)

Resources