首页 > 解决方案 > 即使登录成功,向表添加输入(通过 ASP.NET Web 应用程序和 Azure)也不会在表中注册

问题描述

当我执行我的 asp.net Web 应用程序时,它成功登录,但它不会将数据添加到我设置的 SQL 表中。我已经按照教程here到了一个发球台。一切似乎都可以正常登录,但是当我执行see video时,它似乎是一样的,除了表中没有添加任何内容。没有错误或任何东西。

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

namespace secondTryWebTest
{
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            Label1.Text = ("Congratulations, your name has been added to the query!");
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection secondtryconn = new SqlConnection("Server=tcp:secondtry.database.windows.net,1433;Initial Catalog=SecondTry;Persist Security Info=False;User ID=xxxxxxx;Password=xxxxxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");
        {
            SqlCommand insert = new SqlCommand("EXEC dbo.InsertFullname @Fullname", secondtryconn);
            insert.Parameters.AddWithValue("@Fullname", insertName.Text);

            secondtryconn.Open();
            insert.ExecuteNonQuery();
            secondtryconn.Close();
        }
    }
}

它仅在我直接插入EXEC dbo.InsertFullname @fullname = '(putinputhere)'SQLQuery 时才有效。

如果您还希望我详细说明或添加任何其他内容,请告诉我。

谢谢乔什

标签: c#sqlasp.netazureazure-sql-database

解决方案


根据我的测试,我们可以使用代码向 Azure SQL 表插入数据。我的详细步骤如下。

  1. 创建表
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tblFullname](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Fullname] [nchar](30) NULL,
 CONSTRAINT [PK_tblFullname] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
  1. 创建存储产品
    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    -- =============================================
    -- Author:      <Author, , Name>
    -- Create Date: <Create Date, , >
    -- Description: <Description, , >
    -- =============================================
    CREATE PROCEDURE [dbo].[InsertFullname]
    (
        -- Add the parameters for the stored procedure here
        @Fullname nchar(30)
    )
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON

        -- Insert statements for procedure here

       Insert into dbo.tblFullname (Fullname) values(@Fullname) 
    END
    GO 
  1. 代码

Mywebform.aspx.cs

 public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == true) {

                Label1.Text = ("Great job! Data has  inserted");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var connect = new SqlConnection("Server=tcp:jacksqldemo.database.windows.net,1433;Initial Catalog=sqldemo;Persist Security Info=False;User ID=jack;Password=Password0123!;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");
            {
                SqlCommand insert = new SqlCommand("EXEC dbo.InsertFullname @Fullname",connect);
                insert.Parameters.AddWithValue("@Fullname",insertName.Text);

                connect.Open();

                insert.ExecuteNonQuery();

                connect.Close();

                if (IsPostBack) {

                    insertName.Text = "";
                }

            }
        }
    }

Mywebform.aspx.designers.cs

public partial class WebForm1 {

        /// <summary>
        /// form1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlForm form1;

        /// <summary>
        /// Label1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Label Label1;

        /// <summary>
        /// insertName control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.TextBox insertName;

        /// <summary>
        /// Button1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Button Button1;
    }
  1. 结果

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述


推荐阅读