c# - 即使登录成功,向表添加输入(通过 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 时才有效。
如果您还希望我详细说明或添加任何其他内容,请告诉我。
谢谢乔什
解决方案
根据我的测试,我们可以使用代码向 Azure SQL 表插入数据。我的详细步骤如下。
- 创建表
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
- 创建存储产品
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
- 代码
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;
}
- 结果
推荐阅读
- python - 在按钮单击事件中更新 Bokeh 中的地理可视化
- graphviz - Graphviz 中的循环列表?或如何弯曲边缘
- javascript - 函数返回 ZoneAwarePromise'。为什么?
- javascript - 在 Sails 后端中使用来自 OAuth 响应的 JWT 令牌
- c++ - 字符串向量到 uint8_t C++
- php - 具有多个注入类实例的 DI
- php - 调用 decrypt_string() 时 PHP 脚本无错误退出
- c++ - Qt:带有 QUdpSocket 的 SSDP 在极少数情况下工作
- tcp - 客户端连接时出现 Ncat“错误文件描述符”错误
- css - 分层背景颜色、背景图像和文本