Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文本放入SQL Server 2005数据库_C#_Sql Server 2005_Input - Fatal编程技术网

C# 将文本放入SQL Server 2005数据库

C# 将文本放入SQL Server 2005数据库,c#,sql-server-2005,input,C#,Sql Server 2005,Input,我有一个SQL数据库,1个表,3个列文档ID、文档标题、文档正文 我有一个带有2个输入的aspx页面。。。标题为1,正文为1,提交按钮为1 我究竟如何才能将输入字段中的文本存储在数据库的新行中?我找不到一个简单的。。。具体的答案,不可能那么复杂 <form id="form1" runat="server"> <div style="width: 800px; margin-top: 40px;"> <p style="text-align: left"&g

我有一个SQL数据库,1个表,3个列<代码>文档ID、文档标题、文档正文

我有一个带有2个输入的aspx页面。。。标题为1,正文为1,提交按钮为1

我究竟如何才能将输入字段中的文本存储在数据库的新行中?我找不到一个简单的。。。具体的答案,不可能那么复杂

<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>

头衔

身体


对输入使用ASP.NET服务器控件,而不是


最简单的方法是在
OnClick
处理程序中使用直接的ADO.NET,但这会导致意大利面代码和UI操作(设置和读取例如文本框)与数据访问代码的混合,这不是一个好方法

Anywhere-这里是最简单的方法(同样:不建议实际使用)

当然,从编程的角度来看,使用ORM可能会使事情变得更容易(只需“新建”一个
文档
,设置它的
.Title
.Body
属性,并在其上调用
.Save()
,或者类似的东西),但这些ORM也有一定的学习曲线

另外:如果你想做一些简单到中等复杂的事情,或者你刚刚开始ASP.NET开发,为什么不去看看呢?它包含了许多帮助程序和“包装器”,使处理典型任务变得更加容易——尤其是数据库


请参阅。

SQLServerManagementStudio 2005。VisualStudio2010。
 <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();        
   }
 }
 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();
     }
 }