English 中文(简体)
将案文输入2005年服务器数据库
原标题:Place text into a SQL Server 2005 database

我有一个数据库,有1个表格,有3栏。ID, documentTitle, documentBody

我有一页,有2个投入......1个为身体,1个为 but。

我如何把案文放在投入领域以储存在数据库中的新行? 我无法找到一个简单的......具体答案,根本不可能如此复杂。

<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 as the newest version" /><span> or
        </span><a href>Cancel</a></p>
</div>
</form>
问题回答

最简单的做法是使用直接指定经营实体。 handler>中的网络,但这会导致产生间谍代代代代代代代代代代代代代法,并混合使用(例如,文本箱)和数据存码(not)。

任何方面——此处采用simplest办法(但建议不实际使用)

 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();
     }
 }

当然,使用办公室管理,可能会从方案拟定的角度使事情更加容易(调整“新版”,设定其<代码>。 标题。 Body nature, and calls .Save( on it - or some such as that) - but these ORM s do have a certain learning curve.

此外,如果你重新审视一下简单到中等程度的工作,或者如果你重新加入伙伴关系的话。 NET development - why noteck out Microsoft WebMatrix/a>? 它包含许多帮助者和“治疗者”,使处理典型任务变得更容易——特别是数据库——这样!

利用ASP.NET服务器控制你们的投入,而不是<input>

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

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

你们的法典中也有这样的内容:

 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();        
   }
 }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...