c# - Place text into a SQL Server 2005 database -
c# - Place text into a SQL Server 2005 database -
i have sql database 1 table 3 columns.. documentid, documenttitle, documentbody.
i have aspx page 2 inputs... 1 title 1 body , 1 submit button.
how on earth text within input fields store in new row in database? cannot find simple... concrete reply , there no way complicated.
<form id="form1" runat="server"> <div style="width: 800px; margin-top: 40px;"> <p style="text-align: left"> title</p> <p> <input id="inputtitle" runat="server" type="text" style="width: 100%; padding: 6px; font-size: large" /></p> <p style="text-align: left"> body</p> <p> <textarea id="inputbody" runat="server" style="width: 100%; height: 400px" cols="22" rows="66"></textarea></p> <p> <input id="save" type="submit" onclick="submit_onclick" value="save newest version" /><span> or </span><a href>cancel</a></p> </div> </form>
the simplest utilize straight ado.net in onclick handler - leads spaghetti code , intermingling of ui manipulation (setting , reading e.g. textboxes) , info access code - not approach.
anywhere - here goes simplest approach (again: not recommended real use)
protected void submit_onclick(object sender, eventargs e) { string sqlstmt = "insert dbo.yourtable(documenttitle, documentbody) " + "values(@doctitle, @docbody)"; string connectionstring = webconfigurationmanager.connectionstrings["yourconnectionstring"].connectionstring; using(sqlconnection conn = new sqlconnection(connectionstring)) using(sqlcommand cmd = new sqlcommand(sqlstmt, conn)) { cmd.parameters.add("@doctitle", sqldbtype.varchar, 100).value = tbxtitle.text.trim(); cmd.parameters.add("@docbody", sqldbtype.varchar, 100).value = tbxbody.text.trim(); conn.open(); cmd.executenonquery(); conn.close(); } } of course of study - using orm might create things easier programming perspective (just "new up" document, set .title , .body properties, , phone call .save() on - or that) - these orm's have learning curve, too.
also: if you're looking @ doing simple medium-complexity stuff or if you're getting asp.net development - why not check out microsoft webmatrix? contains lot of helpers , "wrappers" create dealing typical tasks much easier - database, one!
see part 5 of intro tutorial on database development.
c# sql-server-2005 input
Comments
Post a Comment