0

okay so im new to database im still learning so my sql local database is working fine on my pc help needed.

to make it work i had to manually string it everytime

public partial class adminLogin : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\UsersSuren\Documents\Visual Studio 2012\Projects\Shopee mobile\Shopee mobile\App_Data\shopping.mdf;Integrated Security=True");
    int i;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void b1_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "select * from admin_login where username='"+t1.Text+"' and password='"+t2.Text+"'";
        cmd.ExecuteNonQuery();
        DataTable dt = new DataTable();
  • Possible duplicate of [Connection string with relative path to the database file](https://stackoverflow.com/questions/1833640/connection-string-with-relative-path-to-the-database-file) – Alex K. Feb 13 '19 at 14:40
  • And what is your actual question? Please be more specific in the necessary parts and not as detailed at the beginning :) **For Help take a look at "[How to ask](https://stackoverflow.com/help/how-to-ask)"** – Hille Feb 13 '19 at 15:28
  • Welcome to SO. Karthik lol, Try looking at the link that @AlexK. posted in his/her comment. it should help answer your question as there is one post with 80+ votes. – IAmNerd2000 Feb 13 '19 at 15:38

3 Answers3

0

Create an item on app.config like

<add key="connectionString" value="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\UsersSuren\Documents\Visual Studio 2012\Projects\Shopee mobile\Shopee mobile\App_Data\shopping.mdf;Integrated Security=True"/>

and then call it on your adminLogin class like

SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionString"].ToString());

Now you can change the connection in the app.config without changing the code and the need of compiling it again.

0

Replace the absolute path with the metadata tag |DataDirectory| see here for more information on the use of DataDirectory - it's at the bottom.

In your case it should probably be:

"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\shopping.mdf;Integrated Security=True"

I'd also suggest moving this kind of information to a config file.

David Martin
  • 11,764
  • 1
  • 61
  • 74
0

Your question is a bit confusing, but per my understanding, you want to avoid always need to change your connection string path because the project is in different locations, right?

You can use Directory.GetCurrentDirectory(), because it gets the current working directory of the application, and then concatenate it with your connection string.

This will solve your problem of always having to change the connection string manually to the absolute path.

It would look something like this:

SqlConnection con = new SqlConnection($@"Data Source=(LocalDB)\v11.0;AttachDbFilename={Directory.GetCurrentDirectory()}\Shopee mobile\App_Data\shopping.mdf;Integrated Security=True");
grooveplex
  • 2,492
  • 4
  • 28
  • 30
leandro.andrioli
  • 997
  • 5
  • 12