原来: SqlCommand objComm = new SqlCommand(strSQL, cn); objComm.Parameters["@Name"].Value=txbName.Text; objComm.Parameters["@Relationship"].Value=txbRelationship.Text; objComm.Parameters["@NickName"].Value=txbNickName.Text; 改: SqlCommand objComm = new SqlCommand(strSQL, cn); objComm.Parameters.Add["@Name",SqlDbType.Varchar,10].Value=txbName.Text; objComm.Parameters.Add["@Relationship",SqlDbType.Varchar,10].Value=txbRelationship.Text; objComm.Parameters.Add["@NickName",SqlDbType.Varchar,10].Value=txbNickName.Text;
行 26: SqlCommand cmd = new SqlCommand(strSql,cn);用Response.Write(strSql);查询这句话是否符合sql语法,你可以在SQLSERVER2000中插入数据试下。
INSERT INTO photos(photoType,photoComingYear,photoName,photoTime,photoLocation,photoTaker,photoDetail)VALUES('%dd%','%2003%','%dd%','%dddd%','%dddd%','%dddddd%','%ddddddddddddd%') “我很想飞”,根据你的办法,输出了这个东西,然后呢?
按 LesleyChiu(鱼头) 的就可以了
SqlConnection cn = new SqlConnection(strConn);
string strSQL = "INSERT INTO users(Name,Relationship,NickName) VALUES(@Name,@Relationship,@NickName)";
SqlCommand objComm = new SqlCommand(strSQL, cn);
objComm.Parameters["@Name"].Value=txbName.Text;
objComm.Parameters["@Relationship"].Value=txbRelationship.Text;
objComm.Parameters["@NickName"].Value=txbNickName.Text;提示错误:
此 SqlParameterCollection 中未包含带有 ParameterName“@Name”的 SqlParameter。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: 此 SqlParameterCollection 中未包含带有 ParameterName“@Name”的 SqlParameter。
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="C#" runat="server">
void Page_Load()
{
}
void ent_Insert(object sender,EventArgs e)
{
string strPhotoType = Request.Form["txbPhotoType"].ToString();
string strPhotoYear = ddlYear.SelectedItem.Value;
string strPhotoNumber = Request.Form["txbNumber"].ToString();
string strPhotoTakeYear = Request.Form["txbPhotoYear"].ToString();
string strPhotoLocation = Request.Form["txbLocation"].ToString();
string strPhotoTaker = Request.Form["txbTaker"].ToString();
string strPhotoDetail = Request.Form["txbDetail"].ToString();
string strPhotoPreviewPath = @"<a href=photoshow.aspx?type="+strPhotoType+"&year="+strPhotoYear+
"&id="+strPhotoNumber+"><img src=images/picturesandphotos/small/"+
strPhotoType+"/"+strPhotoYear+"/"+strPhotoNumber+".jpg border=0/></a>";
string strConnection = ConfigurationSettings.AppSettings["CNG"];
string strSql = "INSERT INTO photos(photoType,photoComingYear,photoName,photoTime,photoLocation,photoTaker,photoDetail)"+
"VALUES('%"+strPhotoType+"%','%"+strPhotoYear+"%','%"+strPhotoNumber+"%','%"+strPhotoTakeYear+"%','%"+strPhotoLocation+"%','%"+strPhotoTaker+"%','%"+strPhotoDetail+"%')";
SqlConnection cn = new SqlConnection(strConnection);
SqlCommand cmd = new SqlCommand(strSql,cn);
cn.Open();
cmd.ExecuteNonQuery();
lbStatus.Text = "数据插入成功";
cn.Close();
}
</script>
<HTML>
<HEAD>
<TITLE>插入图片数据</TITLE>
</HEAD>
<BODY>
<form runat="server">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><div align="center">
<p><font size="5"><strong>插入图片</strong></font></p>
</div>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><hr noshade></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="10" cellpadding="0">
<tr>
<td valign="top">
<table width="600" border="0" cellspacing="5" cellpadding="0">
<tr valign="bottom">
<td width="100"> <div align="right">图片类型:</div></td>
<td width="500"> <asp:textbox ID="txbPhotoType" runat="server"/> </td>
</tr>
<tr valign="bottom">
<td> <div align="right">加入年份:</div></td>
<td width="500"> <asp:dropdownlist ID="ddlYear" runat="server">
<asp:listitem>2003</asp:listitem>
<asp:listitem>2004</asp:listitem>
<asp:listitem>2005</asp:listitem>
<asp:listitem>2006</asp:listitem>
<asp:listitem>2007</asp:listitem>
</asp:dropdownlist>
年 </td>
</tr>
<tr valign="bottom">
<td> <div align="right">图片序数:</div></td>
<td width="500"> <asp:textbox ID="txbNumber" Width="50" runat="server"/>
.jpg </td>
</tr>
<tr valign="bottom">
<td> <div align="right">拍摄年份:</div></td>
<td width="500"> <asp:textbox ID="txbPhotoYear" Width="50" runat="server"/>
年 </td>
</tr>
<tr valign="bottom">
<td> <div align="right">拍摄地点:</div></td>
<td width="500"> <asp:textbox ID="txbLocation" Width="250" runat="server"/>
</td>
</tr>
<tr valign="bottom">
<td> <div align="right">摄影师:</div></td>
<td width="500"> <asp:textbox ID="txbTaker" runat="server"/> </td>
</tr>
<tr valign="bottom">
<td valign="top"> <div align="right">图片简介:</div>
</td>
<td width="500"> <asp:textbox ID="txbDetail" Height="100" Width="200" TextMode="MultiLine" runat="server"/>
</td>
</tr>
</table></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><hr noshade></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="10" cellpadding="0">
<tr>
<td valign="top"><asp:button ID="btnInsert" Text="插入数据" OnClick="ent_Insert" runat="server"/></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><hr noshade></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="10" cellpadding="0">
<tr>
<td valign="top"><asp:label id="lbStatus" runat="server"/></td>
</tr>
</table>
</form>
</BODY>
</HTML>----------------------------------------------------------------然后,错误提示:“/CNG”应用程序中的服务器错误。
--------------------------------------------------------------------------------将截断字符串或二进制数据。语句已终止。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 将截断字符串或二进制数据。语句已终止。源错误:
行 26: SqlCommand cmd = new SqlCommand(strSql,cn);
行 27: cn.Open();
行 28: cmd.ExecuteNonQuery();
行 29:
行 30: lbStatus.Text = "数据插入成功";
源文件: C:\Inetpub\wwwroot\CNG\o\insertPhoto.aspx 行: 28 堆栈跟踪:
[SqlException: 将截断字符串或二进制数据。
语句已终止。]
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +192
ASP.insertPhoto_aspx.ent_Insert(Object sender, EventArgs e) in C:\Inetpub\wwwroot\CNG\o\insertPhoto.aspx:28
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1263
--------------------------------------------------------------------------------
版本信息: Microsoft .NET 框架版本:1.0.3705.0; ASP.NET 版本:1.0.3705.0
SqlCommand objComm = new SqlCommand(strSQL, cn);
objComm.Parameters["@Name"].Value=txbName.Text;
objComm.Parameters["@Relationship"].Value=txbRelationship.Text;
objComm.Parameters["@NickName"].Value=txbNickName.Text;
改:
SqlCommand objComm = new SqlCommand(strSQL, cn);
objComm.Parameters.Add["@Name",SqlDbType.Varchar,10].Value=txbName.Text;
objComm.Parameters.Add["@Relationship",SqlDbType.Varchar,10].Value=txbRelationship.Text;
objComm.Parameters.Add["@NickName",SqlDbType.Varchar,10].Value=txbNickName.Text;