Question SQL CONNECTIO SAMPLE CODE FAIL.. can any one help

Haseo

Member
Joined
Jul 16, 2012
Messages
16
Programming Experience
Beginner
this is my code for connection... i'm new in VB.Net so please help me for this


Imports System.Data.SqlClient
Public Class Form1




Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnn As New SqlConnection("Data Source=.\SQLExpress, Initial Catalog=\SQLSAMPLE.mdf; Integrated Security=True")
Dim cmm As New SqlCommand("SELECT * FROM sample", cnn)
Try
cnn.Open()
cmm.ExecuteReader()


Catch ex As Exception
MsgBox("Failed")
Finally
cnn.Close()
End Try
End Sub
End Class
 
How about, instead of just showing the message "Failed", you actually look at the exception to see WHY it failed. That's what the exception is for. The IDE is trying to give you information and you're simply ignoring it.

That said, I can see that your connection string is wrong. Are you trying to connect to a database that is already attached to the SQL Server instance or are you trying to attach a file at run time, because you're not doing either properly. If it's the former then you use Initial Catalog and you specify ONLY the name of the database, which means no slash and no file extension, because it's not a file path. If it's the latter then you use AttachDbFileName and specify the FULL path of the data file. If the file is in the program folder then you use |DataDirectory| as the folder path.

Go to http://www.connectionstrings.com for examples of each.
 
How about, instead of just showing the message "Failed", you actually look at the exception to see WHY it failed. That's what the exception is for. The IDE is trying to give you information and you're simply ignoring it.

That said, I can see that your connection string is wrong. Are you trying to connect to a database that is already attached to the SQL Server instance or are you trying to attach a file at run time, because you're not doing either properly. If it's the former then you use Initial Catalog and you specify ONLY the name of the database, which means no slash and no file extension, because it's not a file path. If it's the latter then you use AttachDbFileName and specify the FULL path of the data file. If the file is in the program folder then you use |DataDirectory| as the folder path.

Go to ConnectionStrings.com - Forgot that connection string? Get it here! for examples of each.



sir like this??


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If sqlcnn.State = ConnectionState.Open Then
sqlcnn.Close()
sqlcnn.ConnectionString = "Data Source=.\sqlexpress; integrated security = true; attachdbfilename,=|datadirectory|\SQLSAMPLE.mdf; user instance=true"
sqlcnn.Open()
End If


End Sub
 
Use the code below to get the actual error, so we you can see why it failed
Imports System.Data.SqlClient
Public Class Form1




Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnn As New SqlConnection("Data Source=.\SQLExpress, Initial Catalog=\SQLSAMPLE.mdf; Integrated Security=True")
Dim cmm As New SqlCommand("SELECT * FROM sample", cnn)
Try
cnn.Open()
cmm.ExecuteReader()


Catch ex As Exception
MsgBox(ex.Message.toString)
End Try
End Sub
End Class
 
Back
Top