1

我有一个非常简单的页面,只是想了解如何将文本框中的数据传递到我的程序中。我是一种将参数从后面的代码传递到数据库的新手,我有一页代码是

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:TextBox ID="LastName" runat="server"></asp:TextBox>
        <asp:TextBox ID="FirstName" runat="server"></asp:TextBox>
        <asp:TextBox ID="Phone1" runat="server"></asp:TextBox>
        <asp:TextBox ID="Phone2" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TicketsConnectionString %>" SelectCommand="SELECT * FROM [Employee]"></asp:SqlDataSource>

    </div>
    </form>
</body>
</html>

和aspx代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    string connectionString = ConfigurationManager.ConnectionStrings["TicketsConnectionString"].ConnectionString;

    protected void Button1_Click(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("InsertIntoEmployee", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = LastName.Text;
    cmd.Parameters.Add("@FirstName", SqlDbType.VarChar);
    cmd.Parameters.Add("@Phone1", SqlDbType.VarChar);
    cmd.Parameters.Add("@Phone2", SqlDbType.VarChar);
    cmd.Parameters["@LastName"].Value = LastName.Text;
    cmd.Parameters["@FirstName"].Value = FirstName.Text;
    cmd.Parameters["@Phone1"].Value = Phone1.Text;
    cmd.Parameters["@Phone2"].Value = Phone2.Text;
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    }
}

web.config 看起来像这样

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <connectionStrings>
    <add name="TicketsConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=&quot;C:\Users\Asya\Documents\Visual Studio 2012\WebSites\WebSite6\App_Code\Tickets.mdf&quot;;Integrated Security=True;Connect Timeout=30"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
</configuration>

当我将简单数据传递给我的程序时,它会引发错误

An SqlParameter with ParameterName '@LastName' is not contained by this SqlParameterCollection.

如何解决这个问题?因为在数据库端程序完美运行正如我之前所说的那样,我真的是新手。我解决了以前的问题,但这里有另一个问题

The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.FormatException: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.

Source Error: 


Line 34:         cmd.Parameters["@Phone2"].Value = Phone2.Text;
Line 35:         con.Open();
Line 36:         cmd.ExecuteNonQuery();
Line 37:         con.Close();
Line 38: 

我使用的程序

procedure InsertIntoEmployee
@LastName Nvarchar (25) ,
@FirstName Nvarchar (25) , 
@Phone1 Nvarchar (25) ,
@Phone2 Nvarchar (25)
as 
insert into  Employee  (LastName , FirstName,Phone1,Phone2)values (@LastName,@FirstName,@Phone1,@Phone2);

当我在 Db 端运行 prosedure 时, EXEC InsertIntoEmployee N'петров', N'петр', 99999999,null 它​​工作得很好,但从 asp.net 端它抛出错误

4

4 回答 4

3

如果我错了,请纠正我,但似乎这条线

cmd.Parameters.Add("@LastName ", SqlDbType.Date);

在LastName之后有一个额外的空间。应该是

cmd.Parameters.Add("@LastName", SqlDbType.Date);

反而?

于 2013-08-20T04:29:43.473 回答
2

您需要从“@LastName”中删除空格

就像是

cmd.Parameters.Add("@LastName", SqlDbType.Date);

但这只会将错误转移到嵌套行,因为@FirstName它也不在参数列表中。

事实上,在你有正确的@LastName 之后

所有这 3 行都会导致问题

cmd.Parameters["@FirstName"].Value = FirstName.Text;
cmd.Parameters["@Phone1"].Value = Phone1.Text;
cmd.Parameters["@Phone2"].Value = Phone2.Text
于 2013-08-20T04:31:51.620 回答
2

当然,关于空间的其他答案是正确的。请注意,如果您立即分配参数值,则可以避免此类问题(或至少使它们始终错误;-),如下所示:

cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = LastName.Text;

此外,您用于SqlDbType.Date参数@LastName。除非您有一个非常奇怪的架构,否则这很可能是错误的。因此,我在SqlDbType.VarChar上面使用。

于 2013-08-20T04:35:38.280 回答
1

试试这个

protected void Button1_Click(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("InsertIntoEmployee", con);
        cmd.CommandType = CommandType.StoredProcedure;            
        cmd.Parameters.Add("@LastName", SqlDbType.NVarChar(MAX)).Value = LastName.Text;
         cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar(MAX)).Value = FirstName.Text;
          cmd.Parameters.Add("@Phone1", SqlDbType.NVarChar(MAX)).Value = Phone1.Text;
         cmd.Parameters.Add("@Phone2", SqlDbType.NVarChar(MAX)).Value = Phone2.Text;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

      //  gvQuarterlyReport.DataBind();
    }
于 2013-08-20T04:38:10.103 回答