C# database path -
tomorrow i'll go c# competition. we'll have use databases , our sources tested on different computer can't hardcode connectionstring this:
sqlconnection connection = new sqlconnection(@"c:\users\radu\documents\visual studio 2013\projects\windowsformsapplication12\windowsformsapplication12\database1.mdf");
i compiled code:
private void button1_click(object sender, eventargs e) { string mdfpath = path.combine(application.startuppath, "database1.mdf"); sqlconnection connection = new sqlconnection(@"data source=(localdb)\v11.0;attachdbfilename=" + mdfpath + ";"); connection.open(); string commandstring = "select * conturi"; sqlcommand command = new sqlcommand(commandstring, connection); sqldatareader reader = command.executereader(); while(reader.read()) { messagebox.show(reader.getstring(1) + reader.getstring(2)); } reader.close(); string commandstring1 = "insert conturi (nume, parola) values (@name, @pass)"; sqlcommand command1 = new sqlcommand(commandstring1, connection); command1.parameters.addwithvalue("name", textbox1.text.tostring()); command1.parameters.addwithvalue("pass", textbox2.text.tostring()); command1.executenonquery(); }
it let me read data in database first statement can't insert data db. if hardcode path insertion work.
Comments
Post a Comment