[英]System.Data.SqlClient.SqlException: 'Conversion failed when converting date and/or time from character string.'
[英]SqlException: Conversion failed when converting date and/or time from character string
使用“添加”用户信用卡详细信息的C#网络表单时出现此错误。 以下是我的aspx.cs页面上的“添加信用卡”按钮的代码
protected void Button1_Click(object sender, EventArgs e)
{
//declare and initialize connection object to connect to the database
SqlConnection conn = new SqlConnection(
WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
SqlCommand cmd; //declare a command object that will be used to send commands to database.
conn.Open(); //open a connection to the database
cmd = conn.CreateCommand(); //create a command object
cmd.CommandText = "Insert into CreditCard Values ('" +
txtCCNo.Text + "', '" +
txtFName.Text + "', '" +
txtMidInitial.Text + "', '" +
txtLName.Text + "', '" +
txtExpirationDate.Text + "', '" + txtType.Text + "', " +
txtCVC.Text + ", '" + txtIsDefault.Text + "', '" +
Session["userID"].ToString() + "')";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
conn.Close();
// added for navigation
Response.Redirect("~/selectCC.aspx");
}
以下是.aspx主页
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="AddCC.aspx.cs" Inherits="AddCC" %>
<table style="width: 100%;">
<tr>
<td> <asp:Label ID="Label1" runat="server" Text="First Name"></asp:Label></td>
<td> <asp:TextBox ID="txtFName" runat="server"></asp:TextBox></td>
<td> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="First Name is Required" ControlToValidate="txtFName"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td> <asp:Label ID="Label2" runat="server" Text="Last Name"></asp:Label></td>
<td> <asp:TextBox ID="txtLName" runat="server"></asp:TextBox></td>
<td> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Last Name is Required" ControlToValidate="txtLName"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td> <asp:Label ID="Label3" runat="server" Text="Middle Initial"></asp:Label></td>
<td> <asp:TextBox ID="txtMidInitial" runat="server" MaxLength="1"></asp:TextBox></td>
<td> </td>
</tr>
<tr>
<td> <asp:Label ID="Label4" runat="server" Text="Credit Card No"></asp:Label></td>
<td> <asp:TextBox ID="txtCCNo" runat="server"></asp:TextBox></td>
<td> <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Credit Card is Required" ControlToValidate="txtCCNo"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td> <asp:Label ID="Label5" runat="server" Text="Expiration Date"></asp:Label></td>
<td> <asp:TextBox ID="txtExpirationDate" runat="server"></asp:TextBox></td>
<td> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Expiration Date is Required" ControlToValidate="txtExpirationDate"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td> <asp:Label ID="Label6" runat="server" Text="CVC"></asp:Label></td>
<td> <asp:TextBox ID="txtCVC" runat="server"></asp:TextBox></td>
<td> <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="CVC is Required" ControlToValidate="txtCVC"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td> <asp:Label ID="Label7" runat="server" Text="Type"></asp:Label></td>
<td> <asp:TextBox ID="txtType" runat="server"></asp:TextBox></td>
<td> <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="Type is Required" ControlToValidate="txtType"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td> <asp:Label ID="Label8" runat="server" Text="Default"></asp:Label></td>
<td> <asp:TextBox ID="txtIsDefault" runat="server"></asp:TextBox></td>
<td> </td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="Button1" runat="server" Text="Add Credit Card" OnClick="Button1_Click" OnCommand="Button1_Command" /></td>
<td></td>
</tr>
</table>
我相信该错误即将到来,因为ExpirationDate具有DateTime数据类型,但不确定如何在aspx.cs代码中指定此类型,以便由SQL代码处理。
正确的方法是对插入使用参数化查询,并使用SqlParameter类指定参数值。 它还将处理不同语言的日期格式。 并且可以保护您免受SQL注入的侵害,
// 1. declare command object with parameter
SqlCommand cmd = new SqlCommand("select * from Customers where city = @City", conn);
// 2. define parameters used in command object
SqlParameter param = new SqlParameter();
param.ParameterName = "@City";
param.Value = inputCity;
记得:
首先,即使您正在执行此动态SQL创建,也要始终尝试使用参数,它将保护您免受SQL注入的侵害。
其次,尝试首先获取txtExpirationDate.Text的值,然后将其强制转换为DateTime。 尝试使用标准格式将值发送到sql Query中,这可能是无法识别字符串格式(例如,发送类似“ 2015/03/03”的内容)。
看一下DateTime.Parse()
和DateTime.ParseExact()
方法,了解将字符串解析为日期的方法。
人们所说的有关SQL注入的内容非常重要。 如果您只是通过一个接一个的添加一串字符串来构造自己的sql查询,那么如果人们在输入sql查询的控件中键入精心制作的字符串,那么势必会受到讨厌的攻击。 @Oscar的评论谈到了这种风险(有人可以输入一个值,这会导致整个数据库的删除),他的回答向您展示了如何使用参数。
SqlParameter
机制可以防止这种情况,因为在将参数值替换为@Parameter占位符的查询字符串中时,有一些代码会在替换之前先仔细清理参数值。
如果您有任何更安全的选择,则基本上根本不需要通过串联构造自己的sql字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.