What is wrong with this connection string? - sql

Can any one help me with this connection string. I can't manage how to fix.
Dim constring As String
Dim con As SqlCeConnection
Dim cmd As SqlCeCommand
constring = "(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + \\database.sdf;Password=pswrd;File Mode=shared read"
con = New SqlCeConnection()
con.Open()
Thanks

The string contains a piece of code that will now not be executed. I think you mean:
constring = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\database.sdf;Password=pswrd;File Mode=shared read"

I'm not sure if there are any other problems but definitely the code should be outside the string!
constring = String.Format("Data Source ={0}\\database.sdf;Password=pswrd;File Mode=shared read",(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase))

If you want to use login details:
constring = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + \database.sdf; Password = pswrd"
But you don't have to:
constring = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + \database.sdf; Persist Security Info = false"
I tend to use the full file path:
constring = #"Data Source=C:\...\database.sdf;Persist Security Info=False";

Related

VB.NET - Key word is not supported: provider

When I try to connect to AcessDB, I get the error
"Keyword is not supported: provider".
When I try to change provider, I get another error. When I delete provider tag, I also get an error.
Dim Exists As Boolean = False
Dim ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Name\Documents\Visual Studio 2010\Projects\datagrid\datagrid\pokus.accdb;Persist Security Info=False;"
Dim connection As New SqlConnection(ConnectionString)
Try
connection.Open()
Dim command As SqlCommand = connection.CreateCommand
command.CommandText = "SELECT * FROM studenti"
Dim reader As SqlDataReader = command.ExecuteReader
If reader.HasRows Then
Exists = True
Else
Exists = False
End If
reader.Close()
command.Dispose()
Catch ex As Exception
Console.Write(ex.Message)
Finally
connection.Close()
End Try
SqlConnection, SqlCommand, etc. are SQL Server-specific classes. You can't use them to connect to MS Access.
The documentation for SqlConnection makes this very clear:
Represents an open connection to a SQL Server database.
Consider using the OldDbConnection and related classes instead.
I always write wrapper classes for database connections, and I always recommend the same.
Dim sConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Name\Documents\Visual Studio 2010\Projects\datagrid\datagrid\pokus.accdb;Persist Security Info=False;"
Dim Conn As New OleDbConnection
Conn.ConnectionString = sConnectionString
Conn.Open()
Dim sQuery As String = "SELECT * FROM studenti"
Dim da As New OleDbDataAdapter(sQuery, Conn)
Dim dt As New DataTable
da.Fill(dt)
Conn.Close()

creating a ConnectionString in vb.net

i want to create a connection to my database in my application on vb.net
i stored a procedure called "CTable" that will create tables if they don't exist
im using this code:
Dim strConnection As String
strConnection = "Data Source=Localhost; Initial Calalog=Northwind; Integrated Security=True"
Dim MyConn As SqlConnection
Dim cmd As SqlCommand
MyConn = New SqlConnection(strConnection)
Dim query As String = "EXEC CTable"
cmd = New SqlCommand(query, MyConn)
MyConn.Open()
cmd.ExecuteNonQuery()
MyConn.Close()
but he is giving this error:
" An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll
Additional information: Keyword not supported: 'initial calalog'"
my question is : what should i put in my 'strConnection' variable to be able to execute my CTable Procedure????
You have a typo in the connection string. Try "Catalog" instead of "Calalog" ;)
The word is Catalog not Calalog.
So this should work:
strConnection = "Data Source=Localhost; Initial Catalog=Northwind; Integrated Security=True"
Note that you also have to set the CommandType to CommandType.StoredProcedure.
But always use the Using statement to ensure that unmanaged resources are disposed even on error:
Dim strConnection = "Data Source=Localhost; Initial Catalog=Northwind; Integrated Security=True"
Using MyConn = New sqlclient.SqlConnection()
Using cmd = New SqlClient.SqlCommand("EXEC CTable")
cmd.CommandType = CommandType.StoredProcedure
MyConn.Open()
cmd.ExecuteNonQuery()
End Using
End Using ' also closes the conection
Standard Security
Data Source=serverName\instanceName;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
Trusted Connection
Data Source=serverName\instanceName;Initial Catalog=myDataBase;Integrated Security=SSPI;

vb.net Oracle connection using TNS Name?

I have put my tns connection into the .ora file and am now able to conenct to it using SQL plus and can ping it :tnsping myConn.
I've also added the connection to the ODBC Manager and connecting successfully when tetsing conneciton through the ODBC tool.
now i'm having an issue making a connection to it using vb.net
i've tried the following:
oODBCConnection = New Odbc.OdbcConnection(connStr)
oODBCConnection.Open()
where my connStr is:
Data Source=tns.dev;User Id=MyUser;Password=MyPass;
per: http://www.connectionstrings.com/oracle and http://www.shabdar.org/c-sharp/101-connect-to-oracle-using-asp-net-and-csharp.html
what am i doing wrong? it's telling me i need to specify a driver, how do i do it?
Thank you!
the error i'm getting is:
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Did you checked the tnsnames.ora file? Is there any entry for tns.dev?
http://www.mike-devlin.com/oracle/tnsnames_ora.htm
That is not an oracle error - it sounds like you did not create a system dsn. This code looks like it expects a DSN by name of tns.dev.
That said, I would not use odbc if I could help it. You might want to look at Oracle Data Provider for .net
http://www.oracle.com/technetwork/topics/dotnet/index-085163.html
ODBCConnection was incorrect.
solution:
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim myConnection As New OracleConnection(connStr)
myConnection.Open()
I´ve been using the following code in Vb.net
Dim conn As New Odbc.OdbcConnection
Dim cmd As New Odbc.OdbcCommand
Dim drResult As Odbc.OdbcDataReader
Dim connString As String
Dim QuerySQL As String
connString = "Driver={Microsoft ODBC for Oracle};CONNECTSTRING=(DESCRIPTION=(ADDRESS= (PROTOCOL=TCP)(HOST=ORACLEDB01)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORACLE_INSTANCE_NAME)));Uid=john;Pwd=mypassword;"
QuerySQL = "select first_name, last_name from employees where id = 28"
conn.ConnectionString = connString
conn.Open()
cmd.Connection = conn
cmd.CommandText = QuerySQL
drResult = cmd.ExecuteReader()
While drResult.Read
TextBox1.Text = TextBox1.Text & drResult("last_name") & ", " & drResult("first_name") & Environment.Newline
End While
drResult.Close()

VB.net connection string to sql database

After using the following code no errors are shown, but my database is not updated once I have made a change using my management system application.
Any recommendations ?
Dim constring As String = Application.StartupPath.ToString() + "\mydatabaseName.mdf"
Public c As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=" + constring + ";Integrated Security=True;User Instance=True"
Sub openConnection()
conn.ConnectionString = c
conn.Open()
End Sub
Use the following code:
Dim constring As String = Application.StartupPath.ToString() + "\mydatabaseName.mdf"
Public c As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=" + constring + ";Integrated Security=True;User Instance=True"
Dim conn As New SqlConnection(c)
Dim comm As New SqlCommand
Dim strQuery As New String = "SQL Update Query"
Try
comm.CommandText = strQuery
comm.Connection = conn
conn.Open()
comm.ExecuteNonQuery()
Catch ex As Exception
End Try
Change your connection string to:
"Data Source=.\SQLEXPRESS;AttachDbFilename=" + constring + ";Initial Catalog=mydatabaseName;Integrated Security=True;User Instance=True"
Using InitialCatalog helps when there are more than one databases.

What is wrong on connection string

I am trying to connect database.sdf on same director. Using following code but gives me connection error. What I am doing wrong. Please help me.
Dim connstring As String
Dim con As SqlCeConnection
Dim command As SqlCeCommand
connstring = "Persist Security Info = False; Data Source = '.\database.sdf', Password = 'pswrd', File Mode = 'shared read'"
con = New SqlCeConnection
con.Open()
command = New SqlCeCommand("select * from users where Name=? and Password=?", con)
I think you're missing some code... or maybe that's the problem, you never bind your SqlCeConnection to connstring
Dim con As SqlCeConnection
Dim command As SqlCeCommand
con = New SqlCeConnection("Persist Security Info=False;Data Source=.\database.sdf;Password=pswrd;File Mode=shared read")
con.Open()
command = New SqlCeCommand("select * from users where Name=? and Password=?", con)
You do not need the single quotes (') in the different parts of the connection string, and you should be using a semi-colon (;) to separate the different values.
"Persist Security Info = False; Data Source = .\database.sdf; Password = pswrd; File Mode = shared read;"
Apart from that, you do not appear to be using the connection string in your code. You should be using it to open the connection:
con = New SqlCeConnection(connstring)
Check out Connection Strings for great connection string assitance.
It looks like your line:
connstring = "Persist Security Info = False; Data Source = '.\database.sdf', Password = 'pswrd', File Mode = 'shared read'"
is using both "," and ";" to separate the parameters. Update then all to use ";"