c# - SQL database directory string not working - have to define full file path -


i have created application underlying database needs put onto cd exe. however, of sql querys work on database when use full file path of database in connection string.

for example, have create new user form should insert new user database. works when use connection string 1, not connection string 2.

which not ideal because when else uses application on different pc want application in current directory (where database should be).

i have tried various solutions no ovail.

any ideas?

much appreciated

connection string 1

string connection = @"data source=.\sqlexpress;attachdbfilename=c:\users\nick\documents\visual studio 2010\projects\debenhamsprojectoffice v.01\debenhamsprojectoffice v.01\debenhamsprojectofficedatabase.mdf;integrated security=true;connect timeout=30;user instance=true"; 

connection string 2

string connection = @"data source=.\sqlexpress;attachdbfilename=|datadirectory|\debenhamsprojectofficedatabase.mdf;integrated security=true;user instance=true"; 

create new user form code

// create user click event     private void btncreateuser_click(object sender, eventargs e)     {         string username = txtusername.text;         string password = txtpassword.text;         string sqlquery;          if (username.length < 5 || password.length < 5)         {             messagebox.show("username & password must more 5 characters");         }         else         {             string connection = @"data source=.\sqlexpress;attachdbfilename=c:\users\nick\documents\visual studio 2010\projects\debenhamsprojectoffice v.01\debenhamsprojectoffice v.01\debenhamsprojectofficedatabase.mdf;integrated security=true;connect timeout=30;user instance=true";             sqlconnection cn = new sqlconnection(connection);             cn.open();             sqlquery = "insert users (username, password) values ('" + username + "','" + password + "')";             try             {                 sqlcommand command = new sqlcommand(sqlquery, cn);                 command.executenonquery();                 messagebox.show("user added");             }             catch (exception ex)             {                 messagebox.show(ex.message);             }             txtusername.clear();             txtpassword.clear();             cn.close();         }     } 


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -