הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Thursday, April 21, 2016 12:06 PM
Imports System.Data.SqlClient
Imports System.Data
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
Public i As String = Environment.MachineName.ToString()
Public conn As New SqlConnection("server=192.168.1.6,1433; database=Students_MAM12A; User ID=sa; Password=123")
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Cursor.Current = Cursors.WaitCursor
Try
If conn.State = ConnectionState.Closed Then
conn.Open()
Cursor.Current = Cursors.Default
MsgBox("connected")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
conn.Close()
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Dim connectionString As String = conn.ToString()
Dim sql As String = "SELECT * FROM Students_MAM12A"
Dim connection As SqlConnection = New SqlConnection(connectionString)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(sql, conn.ToString())
Dim ds As DataSet = New DataSet()
connection.Open()
dataadapter.Fill(ds, "FirstName")
connection.Close()
DataGridView1.DataSource = ds
End Sub
End Class
here is my code, i have no idea why is this error occuring:
Format of the initialization string does not conform to specification starting at index 0:
the error is on this line:
dim connection as sqlconnection = New sqlconnection (connectionstring)
i am using SQL server 2014.
All replies (3)
Thursday, April 21, 2016 7:22 PM ✅Answered
The error indicates that the connection string is incorrect. See below link for examples:
http://www.connectionstrings.com/sql-server/
Paul ~~~~ Microsoft MVP (Visual Basic)
Thursday, April 21, 2016 9:28 PM ✅Answered
As Paul said, you don't have a properly formatted connection string for SQL Server. Try:
"Data Source=192.168.1.6,1433;Initial Catalog=Students_MAM12A;Persist Security Info=True;User ID=sa;Password=123"
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
Thursday, April 21, 2016 12:38 PM
You simply use the name of that conn.class (what the ToString default does)
Use
conn.connectionstring
Although I find it a very difficult way to get the connectionstring, why not simply use conn in the adapter..
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(sql, conn)
Success
Cor