开发者

Place text into a SQL Server 2005 database

I have a SQL database with 1 table with 3 columns.. documentID, documentTitle, documentBody.

I have an aspx page with 2 inputs... 1 for title 1 for body and 1 submit button.

How on earth do I just get the text inside the input fields to store in a new row in the database? I cannot find a simple... concrete answer and there is no way that it is that complicated.

<form id="form1" runat="server">
<div style="width: 800px; margin-top: 40px;">
    <p style="t开发者_Go百科ext-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 as the newest version" /><span> or
        </span><a href>Cancel</a></p>
</div>
</form>


The simplest would be to use straight ADO.NET in the OnClick handler - but that leads to spaghetti code and intermingling of UI manipulation (setting and reading e.g. textboxes) and data access code - which is not a good approach.

Anywhere - here goes the simplest approach (again: not recommended for real use)

 protected void submit_onclick(object sender, EventArgs e)
 {
     string sqlStmt = "INSERT INTO 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 - using an ORM might make things easier from a programming perspective (just "new up" a Document, set its .Title and .Body properties, and call .Save() on it - or something like that) - but these ORM's do have a certain learning curve, too.

Also: if you're looking at doing simple to medium-complexity stuff or if you're just getting into ASP.NET development - why not check out Microsoft WebMatrix? It contains a lot of helpers and "wrappers" that make dealing with typical tasks much easier - especially the database, for one!

See part 5 of the intro tutorial on database development.


Use ASP.NET server controls for your inputs, instead of an <input>.

 <asp:Button runat="server" Text="Submit" id="sub" OnClick="SaveDetails" />

 <asp:TextBox runat="server" id="txtBody" />
 <asp:TextBox runat="server" id="txtTitle" />

Try something like this in your code-behind:

 protected void SaveDetails(object sender, EventArgs e) {

   using (var conn = new SqlConnection("Data Source=YourServerName;Initial Catalog=YourDatabaseName;Integrated Security=True;"))
   using (var cmd = conn.CreateCommand())
   {
        conn.Open();
        cmd.CommandText = @"INSERT INTO docs (documentTitle, documentBody) 
                            VALUES (@title,@body);"; 
        cmd.Parameters.AddWithValue("@title", txtTitle.Text.Trim());
        cmd.Parameters.AddWithValue("@body", txtBody.Text.Trim());

        cmd.ExecuteNonQuery();        
   }
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜