textarea 文本到 sql 列
Posted
技术标签:
【中文标题】textarea 文本到 sql 列【英文标题】:textarea text into sql column 【发布时间】:2011-10-16 16:11:33 【问题描述】:如果我使用文本框,我可以毫无问题地将数据导入 sql。我想使用带有自动换行功能的 textarea 等等。但是当我将<input type="text" id="au_id">
更改为<textarea name="au_id">
时,我无法获得下面列出的au_id.Value
代码的.value
。
我想要的只是将单行文本框换成多行文本区域,并且仍然可以通过单击将我的行发布到 sql 中。一些名称/ID 对它们的用途没有意义,我从 Microsoft 的网站复制了大部分代码,并在进行过程中进行了更改。
代码:
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text"%>
<html>
<head>
<style type="text/css">
#au_id
height: 103px;
</style>
</head>
<script language="VB" runat="server" >
Dim MyConnection As SqlConnection
Sub Page_Load(Src As Object, e As EventArgs)
' Create a connection to the "EMR" SQL database located on
' the local computer.
MyConnection = New SqlConnection("server=localhost;" _
& "database=EMR;Trusted_Connection=Yes")
' Check whether this page is a postback. If it is not
' a postback, call a custom BindGrid function.
If Not IsPostBack Then
BindGrid()
End If
End Sub
' Implement an AddAuthor_Click function. This function does some data
' validation on the input form and builds a parameterized command containing
' all the fields of the input form. Then it executes this command to the
' database and tests (using the try command) whether the data was added.
' Finally, it rebinds the DataGrid to show the new data.
Sub AddAuthor_Click(ByVal Sender As Object, ByVal e As EventArgs)
Dim myCommand As SqlCommand
Dim insertCmd As String
If (au_fname.Value = "" Or au_lname.Value = "" _
Or phone.Value = "") Then
Message.InnerHtml = "ERROR: Null values not allowed for " _
& "Author ID, Name or Phone"
Message.Style("color") = "red"
BindGrid()
Exit Sub
End If
' Build a SQL INSERT statement string for all the input-form
' field values.
insertCmd = "insert into VisitData values (@Subjective, @Objective, @Assessment," _
& "@Plan, @HT, @WT, @BP, @ServiceDate, @Diagnosis);"
' Initialize the SqlCommand with the new SQL string.
myCommand = New SqlCommand(insertCmd, myConnection)
' Create new parameters for the SqlCommand object and
' initialize them to the input-form field values.
myCommand.Parameters.Add(New SqlParameter("@Subjective", _
SqlDbType.VarChar, 8000))
myCommand.Parameters("@Subjective").Value = au_id.Value
myCommand.Parameters.Add(New SqlParameter("@Objective", _
SqlDbType.VarChar, 8000))
myCommand.Parameters("@Objective").Value = au_lname.Value
myCommand.Parameters.Add(New SqlParameter("@Assessment", _
SqlDbType.VarChar, 8000))
myCommand.Parameters("@Assessment").Value = au_fname.Value
myCommand.Parameters.Add(New SqlParameter("@Plan", _
SqlDbType.Char, 8000))
myCommand.Parameters("@Plan").Value = phone.Value
myCommand.Parameters.Add(New SqlParameter("@HT", _
SqlDbType.VarChar, 40))
myCommand.Parameters("@HT").Value = address.Value
myCommand.Parameters.Add(New SqlParameter("@WT", _
SqlDbType.VarChar, 20))
myCommand.Parameters("@WT").Value = city.Value
myCommand.Parameters.Add(New SqlParameter("@BP", _
SqlDbType.Char, 10))
myCommand.Parameters("@BP").Value = state.Value
myCommand.Parameters.Add(New SqlParameter("@ServiceDate", _
SqlDbType.Char, 10))
myCommand.Parameters("@ServiceDate").Value = zip.Value
myCommand.Parameters.Add(New SqlParameter("@Diagnosis", _
SqlDbType.VarChar, 20))
myCommand.Parameters("@Diagnosis").Value = contract.Value
myCommand.Connection.Open()
' Test whether the new row can be added and display the
' appropriate message box to the user.
Try
myCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>Record Added</b><br>"
Catch ex As SqlException
If ex.Number = 2627 Then
Message.InnerHtml = "ERROR: A record already exists with " _
& "the same primary key"
Else
Message.InnerHtml = "ERROR: Could not add record, please " _
& "ensure the fields are correctly filled out"
Message.Style("color") = "red"
End If
End Try
myCommand.Connection.Close()
BindGrid()
End Sub
' BindGrid connects to the database and implements a SQL
' SELECT query to get all the data in the "Authors" table
' of the database.
Sub BindGrid()
Dim myConnection As SqlConnection
Dim myCommand As SqlDataAdapter
' Create a connection to the "EMR" SQL database located on
' the local computer.
myConnection = New SqlConnection("server=localhost;" _
& "database=EMR;Trusted_Connection=Yes")
' Connect to the SQL database using a SQL SELECT query to get all
' the data from the "Authors" table.
myCommand = New SqlDataAdapter("SELECT * FROM VisitData", _
myConnection)
' Create and fill a new DataSet.
Dim ds As DataSet = New DataSet()
myCommand.Fill(ds)
' Bind the DataGrid control to the DataSet. will remain hidden
MyDataGrid.DataSource = ds
MyDataGrid.DataBind()
End Sub
</script>
<body style="font: 10pt verdana">
<form id="Form1" runat="server">
<h3><font face="Verdana">Inserting Visit Data</font></h3>
<table >
<tr>
<td valign="top" nowrap="nowrap">
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="700"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
Visible="false"
EnableViewState="false"
/>
</td>
<body style="font: 10pt verdana">
<td valign="top">
<table style="font: 8pt verdana">
<tr>
<td colspan="2" bgcolor="#aaaadd" style="font:10pt verdana">
Add Visit Info:</td>
</tr>
<td>Subjective: </td>
<td>
<input type="text" id="au_id" value="" runat="server" maxlength="8000"
style="overflow: scroll; white-space: normal;" />
</td>
<tr>
<td nowrap>Objective: </td>
<td><input type="text" id="au_lname" value=""
runat="server"></td>
</tr>
<tr nowrap>
<td>Assessment: </td>
<td><input type="text" id="au_fname" value=""
runat="server"></td>
</tr>
<tr>
<td>Plan: </td>
<td><input type="text" id="phone" value=""
runat="server"></td>
</tr>
<tr>
<td>HT: </td>
<td><input type="text" id="address"
value="" runat="server"></td>
</tr>
<tr>
<td>WT: </td>
<td><input type="text" id="city" value=""
runat="server"></td>
</tr>
<tr>
<td>BP: </td>
<td>
<input type = "text" id="state" runat="server">
</td>
</tr>
<tr>
<td nowrap>Date of Service: </td>
<td><input type="text" id="zip" value=""
runat="server"></td>
</tr>
<tr>
<td>Diagnosis: </td>
<td>
<Input type="text" id="contract" value="" runat="server">
</td>
</tr>
<tr>
<td></td>
<td style="padding-top:15">
<input id="Submit1" type="submit" OnServerClick="AddAuthor_Click"
value="Add Visit" runat="server">
</td>
</tr>
<tr>
<td colspan="2" style="padding-top:15" align="center">
<span id="Message" EnableViewState="false"
style="font: arial 11pt;" runat="server"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
【问题讨论】:
【参考方案1】:要更改为文本区域,您需要保留id="au_id"
和runat="server"
属性
<input type="text" id="au_id" value="" runat="server" maxlength="8000" style="overflow: scroll; white-space: normal;" />
变成
<textarea runat="server" id="au_id" rows="4" cols="8">default text</textarea>
注意:删除最大长度
要从您的 VB.NET 代码中访问 textarea 值:
Reponse.Write(au_id.value)
【讨论】:
@Connie Chichuk - 请务必接受 hamlin11 的回答,以便他获得荣誉(和代表)。 :)【参考方案2】:在我自己遇到这个问题并花了一些时间尝试通过许多论坛帖子的解决方案解决问题后,我设法解决了同样的问题。在我检查的所有论坛中,问题是使用 $_POST
php 超全局变量将字符串值从 textarea 获取到 sql INSERT
语句中
我能看到它确实有效且不会引发任何错误的唯一方法是在编写 INSERT
语句时忽略列名。
所以而不是:
"INSERT INTO tbl_dbTable ( Colummn1, Colummn2...) VALUES( '$_POST[textarea]', 'Value2'..)"
简单地使用:
"INSERT INTO tbl_dbTable VALUES( '$_POST[textarea]', 'Value2'..)"
唯一的缺点是您必须随后列出表中所有列的值,但它解决了将 textarea 值放入 sql 列的问题。
希望对你有帮助
【讨论】:
以上是关于textarea 文本到 sql 列的主要内容,如果未能解决你的问题,请参考以下文章