Help making a database connection in asp.net?

Hi, I have this in my web.config file:

 
<connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS; Integrated Security=SSPI; AttachDBFilename=|DataDirectory|\aspnetdb.mdf; User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

Open in new window


But I'm not sure what my Data Source is supposed to be, or my Initial Catalog, or if the AttachDBFilename is correct.  I set up a database with sql server on my computer and I want to connect it to that.  How do I do that?
FairyBusinessAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale BurrellDirectorCommented:
You haven't really given enough information to go on. But this link should give you what you need to know.

http://www.connectionstrings.com/
0
FairyBusinessAuthor Commented:
Thanks that website does help!

But i still dont know how do I figure out what my server address is?  Is there some place in sql server I found it?

Data Source=myServerAddress;
0
r1tman2003Commented:
here is a working connection string that is in use and working for me now: (SQL Express)

Conn.Open "Provider=sqloledb;Server=localhost\sqlexpress;DATABASE=dbname;UID=dbUsername;PWD=dbPassword"

Open in new window

0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

r1tman2003Commented:
sorry about the formatting.

Conn.Open "Provider=sqloledb;Server=localhost\sqlexpress;DATABASE=dbname;UID=dbUsername;PWD=dbPassword"
0
Dale BurrellDirectorCommented:
You've still not provided enough information to answer that question - as you may have seen looking at the connection strings it depends on the database type and the way the database was installed.

It for example you are using SQL Server 2005 installed using defaults then Server=localhost.
0
FairyBusinessAuthor Commented:
@r1tman2003  Your format is different than mine. I am not sure how to use that.  Also, why dont you have a data source specified?   What is the Provider and Server for?  
0
r1tman2003Commented:
i saw sql express in your example so I went and copied the string from my config file...

DATABSE, UID and PASSWORD are in order; database name inside sql server express, then the username and password for the account in that database. those are the 3 things to edit.

0
r1tman2003Commented:
I am not sure about the order, but you need(ed) to go into sql server management studio(SSMS) and create the database. Then you have to go to the section called security UNDER the databases section(as there is also a branch called security off the database, so dont confuse the 2)... create a user for the database, and then assign the ne user access to the database. I agree with tthe guy above, a little more info would help...

How did you setup the database, include as many details as possible.
0
FairyBusinessAuthor Commented:
Well, I'm not sure what information to provide but I am using Visual Studio 2010.

Ok, I went into sql server studio and this is my server name and user name: MSI_K9MM_V\TANYA

I went to the security section, found my user (me), and right clicked and viewed the properties.  It says I have no password and the radio button to switch to a SQL Server Authentication is grayed out (can't click on it).  So I'm not sure how to set a password.  Anyone know how?
0
Dale BurrellDirectorCommented:
What version of SQL server would help, whether the database is located in the app_data folder etc. If you are running on the same windows machine rather than using a username and password use Integrated Security (see the link for how to express that depending on your database etc).
0
ddayx10Commented:
It sounds like you've used some wizard or another to create a database (sqlexpress) using Visual Studio.

VS has automagically created a reference (which likely works) in the web.config for you.

You can now use the web.config reference to connect to your database.

You mention some confusion about what "datasource" to use. There are several options in this area. Usually a new user following a tutorial will learn about the sqldatasource and/or sqldataadapter models.

This link will show you how you might use a sqldatasource and how it references the same type of connectionstring you have in place:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.connectionstring.aspx#Y1214

You will need to have created a table with some fields in it to utilize your connectionstring and a datasource. The link should give you some idea on this as well.

In order to manage your sqlexpress database (see info, create/edit tables) you can utilize Visual Studio's "Server Explorer". Ctl-Alt-S if you dont see it.

Your database connection is likely already in there because of how you created it. You can expand this to get a view of your sqlexpress database and its corresponding tables and their data.

There are 10 ways to do everything in the arena. I'm just taking a few educated guesses that may help you along with what you've started already.
0
FairyBusinessAuthor Commented:
Ok, I went to the help section of sql server and it says this:

Microsoft SQL Server Management Studio                                    11.0.1440.19
Microsoft Data Access Components (MDAC)                                    6.0.6002.18005
Microsoft MSXML                                    3.0 4.0 5.0 6.0
Microsoft Internet Explorer                                    9.0.8112.16421
Microsoft .NET Framework                                    4.0.30319.239
Operating System                                    6.0.6002

I am using the same computer for both sql server and visual studio.  I am not sure if the database is located in the app_data folder or not.  (how can I tell?)
server-info.png
0
FairyBusinessAuthor Commented:
This was so much more simple in php lol

Ok, so at the bare minimum all I need is my server address, username, and password?
0
ddayx10Commented:
Look in the app_data folder, right click and hit refresh on it.

Are you following a book or a set of instructions, or are you just trying to figure out Visual Studio/ASP.NET by winging it?

PHP and MySql... You're not in Kansas anymore Dorothy.

So you can use Visual Studio Server Explorer pane OR Microsoft SQL Server Management Studio to work with your SQLExpress database.

None of the rest of that stuff you posted is important.

Creating databases using SQL Server Management Studio Express
http://mrbool.com/p/Creating-databases-using-SQL-Server-Management-Studio-Express/4672

Connecting to Database SQL Express 2005 with VS 2005
http://www.codeproject.com/KB/database/Connecting_to_SQL_Express.aspx

(doesn't matter the version its just this easy)

Working with Databases in ASP.NET 2.0 and Visual Studio 2005
http://www.4guysfromrolla.com/articles/110905-1.aspx

I know this stuff is old but its where the easy stuff is explained.

I could post this stuff all night but its better to know what approach you are taking etc before making too many suggestions.
app-data.jpg
0
Alfred A.Commented:
What is the name of the database you are trying to connect to?  Try replacing <your database name> below and try it out.

Note:  If you are using Windows Authentication, then try the one below:

<connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS; Integrated Security=SSPI; data source=MSI_K9MM_V\TANYA;initial catalog=<your database name>"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

If you are using something else like SQL Server Authentication, try the one below.  Note replace <your username> and <your password> with your own username and password.  

<connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS; data source=MSI_K9MM_V\TANYA;initial catalog=<your database name>; user id=<your username>; password=<your password>;"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

For example:  If database name is "Northwind" and username is "admin" and password is "Testing789" then it should look like:

<connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS; data source=MSI_K9MM_V\TANYA;initial catalog=Northwind; user id=admin; password=Testing789;"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
0
FairyBusinessAuthor Commented:
My database name is auroriella.

Look at the attached iamges please to see what I did.  I want to test the connection, but why am I getting all the red underlining?  I am following a tutorial from Pro ASP.NET 4 in C# 2010 (fourth edition)
web-config.png
conn.png
0
FairyBusinessAuthor Commented:
the first image was a screen shot I took when I hadn't finished messing around with the connection in the web_config file. Here is how I left it:


web-config.png
0
FairyBusinessAuthor Commented:
Ok, I figured out why there was red underlining and I fixed it.

But I am wondering is there a way that allows me to not repeat information that is already in my web.config file?

This is in my web.config file:

  <connectionStrings>
    <add name="connection"
         connectionString="data source=.\SQLEXPRESS; Integrated Security=SSPI; data source=MSI_K9MM_V\TANYA;
         initial catalog=auroriella providerName=System.Data.SqlClient" />  
  </connectionStrings>


Could I take the name "connection" or the connectionString and use those names to replace more coding in a .cs file?

So instead of:

        SqlConnection conn = new SqlConnection("Data Source=(MSI_K9MM_V\TANYA);Initial Catalog=auroriella;Integrated Security=SSPI");

could I have

SqlConnection conn = new SqlConnection(connectionString);

or

SqlConnection conn = new SqlConnection(connection);

to use as variables from the web.config file?
0
FairyBusinessAuthor Commented:
It doesn't like my \ in my data source though:

conn2.png
0
Dale BurrellDirectorCommented:
You use the connection string from the web.config file as follows:

SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);

Your web.config file is missing a couple of key double quotes it should read:

<add name="connection"
         connectionString="data source=.\SQLEXPRESS; Integrated Security=SSPI; data source=MSI_K9MM_V\TANYA;
         initial catalog=auroriella" providerName="System.Data.SqlClient" />
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FairyBusinessAuthor Commented:
thanks, its connecting now!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.