简体   繁体   中英

Getting data from a table to ASP.NET web application

I'm using Visual Studio 2010 to build an ASP.NET web application, I'm working on dynamically populating (part) of the site map from information in a database. Right now I just have a dummy table in my App_Data folder, called DrugTest.mdf. The table is just called DrugTest1, which only has one field, DrugName. Where I'm hitting a wall is actually getting the data out of that table. Part of what I'm confused about is the connection string. I've looked at a lot of different information about connection strings, most notably http://www.connectionstrings.com/ but I'm a little confused as to how to actually apply said information to this project.

EDIT: I'm using SQL Server 2008 RC.

For example: Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Password and User Id are pretty self-explanatory, but as far as I know I didn't get either one of those, I just added a table to the App_Data folder and filled it with dummy data. ServerAddress is a little confusing, because this information isn't really stored on a server, it's just stored locally. And I'm honestly not sure what Initial Catalog means. Here's the code to populate the sub-tree. You'll notice the connection string is left blank.

string connString = ""; // get the connection string
string commandString = "SELECT drugName FROM DrugTable1";

SqlConnection connection = new SqlConnection(connString); // connect to db
SqlCommand command = new SqlCommand(commandString, connection); // set up the command
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet drugs = new DataSet();
adapter.Fill(drugs);

foreach (DataRow row in drugs.Tables[0].Rows)
{
    string drugName = row["Name"] + "";
    SiteMapNode node = new SiteMapNode(this, drugName,
                        "~/PlaceHolderUrl?path=" + drugName,
                        drugName);
    AddNode(node, root);
}

Furthermore, I've got a nagging suspicion that I'm not going about this the right way. I think this will be the proper implementation once the database is up and running, but for right now I just want to get it working so it's ready to go - just slap in the proper connection string and table/field names.

So, finally, my question(s): How would I go about connecting to this local table? What format should my connection string be? I noticed there's a lot of them. Is there a better way to do this/am I doing this wrong?

Try replacing the Initial Catalog portion of your connection string with AttachDbFilename=|DataDirectory|DrugTest.mdf .

Also, if you're using SQL Server Express, you might need to include the instance in the Data Source, so might try Data Source=mySeverAddress\\SQLExpress , where SQLExpress is the instance name.

BTW, at the http://www.connectionstrings.com site, you can find this information in the SQL Server 2008 page if you scroll down a bit to the section titled "Attach a database file, located in the data directory, on connect to a local SQL Server Express instance."

Another way of getting the right connection string check this out in the ServerExplorer window

  • On the Menu click on View->Server Explorer
  • In the Server Explorer window locate DrugTest.mdf
  • Right click the file and select Properties

在此输入图像描述

  • You can see the right connection string in the properties
  • Copy the connection string and use

Note: that the file location was hard-coded. You might need to use |DataDirectory| later

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM