3

How to: connectionString?

Hopefully a simple question, but I am too new to Visual Basic to understand : http://msdn.microsoft.com/en-us/library/d7469at0.aspx...

I am writing an app in VB and trying to connect it to a "Local Database" / "Dataset".

I received help earlier today to get the code listed below and it appears it will work just fine, except I have no idea how to connect my application to my dataset. From the reading I have been doing, it seems the connectionString would be to connect it to a database that was created with SQL Server. ???

On my VB Windows Form Application, I simply did "Add New Item" then "Local Database" and it asked me what type of database model and I selected "Dataset".

I only have two tables and just need to be able to connect to them. Here is the code or at least the idea of what I would like to do. Please help

    Using sqlCon = New SqlConnection(connectionString)
        sqlCon.Open()
        Dim sqlText = "UPDATE appTable SET clickCount + 1 " & _
            "WHERE appName = @name"
        Dim cmd = New SqlCommand(sqlText, sqlCon)
        cmd.Parameters.AddWithValue("@name", appName)
        cmd.ExecuteNonQuery()
    End Using

I am so sorry for this poorly phrased question, I have been reading and trying to learn this for too long and have frustrated myself. I come from Python with a lot less syntax and "rules" and I feel I could be way farther on this project and to be stuck on simply connecting to the "database" has me beyond frustrated :/

Thanks for assistance in advance.

answer : There is an App.config file in my Solution Explorer with the connectionString Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\gadgetDatabase.mdf;Integrated Security=True

1
  • You should not answer your question inside your question. Post the answer as an answer yourself. I did it for you this time. Commented Apr 21, 2013 at 0:38

8 Answers 8

6

Try

Dim connectionString AS String = "Server=my_server;Database=name_of_db;User Id=user_name;Password=my_password"

And replace my_server, name_of_db, user_name and my_password with your values.

then Using sqlCon = New SqlConnection(connectionString) should work

also I think your SQL is wrong, it should be SET clickCount= clickCount + 1 I think.

And on a general note, the page you link to has a link called Connection String which shows you how to do this.

Sign up to request clarification or add additional context in comments.

3 Comments

I really appreciate it... Where can I go to get my server name and DB name? Don't get me wrong, I know some SQL. And If I had created the DB with SQL Server, I would know right where to go. But since it's a Local Database/Dataset... I have no idea where to find it
Do NOT hard code connection string into the code. Geeze Louise. See Linus answer below.
Without getting into a philosophical discussion, surely the place you code it is dependent upon the use case in question. "Always do this or don't always do this" is never a good suggestion. Somethings make maintenance easier/harder, some make security easier/harder some make some other things easier/harder. It can absolutely be reasonable to do this as much as is could be unreasonable to do this. Good practice like all thing is contextual and within defined boundaries
2

Standard Security:

Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

Trusted Connection:

Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

will be glad if it helps.

Regards.

Comments

1

Set the connection string in your config file:

<connectionStrings>
    <add name="ConnString"
         connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\gadgetDatabase.mdf;Integrated Security=True" />
</connectionStrings>

2 Comments

Since this is a Dataset/Local Database and not a full blown database created by SSMS, do I have an instance?
Is it a mdf file? In that case use something like Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf. Here is a list of common strings: connectionstrings.com/sql-server-2012
0

May it will help for u. U should use (localdb).

LocalDB automatic instance

Server=(localdb)\v11.0;Integrated Security=true;

LocalDB automatic instance with specific data file

Server=(localdb)\v11.0;Integrated Security=true; AttachDbFileName=C:\MyFolder\MyData.mdf;

Comments

0

The Connection String Which We Are Assigning from server side will be same as that From Web config File. The Catalog: Means To Database it is followed by Username and Password And DataClient The New sql connection establishes The connection to sql server by using the credentials in the connection string.. Then it is followed by sql command which retrives the required data in the dataset and then we assing them to required variables or controls to get the required task done

Comments

0

Use the following Imports

Imports System.Data.SqlClient
Imports System.Data.Sql

Public SQLConn As New SqlConnection With {.ConnectionString = "Server=Desktop1[enter image description here][1];Database=Infostudio; Trusted_Connection=true;"}

Full string: enter image description here

Comments

0
Imports System.Data.SqlClient
Imports System.Data.Sql
Imports System.IO
Imports System.Configuration
Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
Dim cn As New SqlConnection(connectionString)
Dim cmd As New SqlCommand
Dim dr As SqlDataAdapter

Comments

0
             if (reader.HasRows)
            {
                while (reader.Read())
                {
                    comboBox1.Items.Add(reader.GetString(0));
                }
            }
            reader.Close();

            MySqlDataReader reader1 = cmd1.ExecuteReader();
            if (reader1.HasRows)
            {
                while (reader1.Read())
                {
                    listBox1.Items.Add(reader1.GetString(0));
                }
            }
            reader1.Close();

1 Comment

this code might solve the problem, but it would be better if you explain how

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.