无法从我的ASP网站表单向SQL Server数据库添加数据



我对SQL数据库和;C#语言,我不能从我的web应用程序向我的SQL数据库添加数据,尽管一切都做得正确?我目前正在Azure SQL&C#;一直在使用以下培训视频:

https://www.youtube.com/watch?v=POWm4EfU9bA

我正在使用MS Visual Studio和MS SQL Server Management Studio,我也在使用Azure Web+SQL服务,我已经打开了防火墙,允许我的客户端IP地址通过。

有人能从我的代码中找出为什么我的网络表单中的数据没有添加到我的数据库中吗?这是我的编码,它"显然"从我的前端web应用程序中的表单向我的SQL数据库添加了数据:

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace bluecitywebapplication
{
public partial class WebForm1 : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
Label1.Text = ("Great job!");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection stormconn = new SqlConnection("Server=tcp:bluecitydb.database.windows.net,1433;Initial Catalog=xxx;Persist Security Info=False;User ID=xxx;Password=xxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");
{
SqlCommand insert = new SqlCommand("EXEC dbo.InsertFullname @Fullname", stormconn);
insert.CommandType = System.Data.CommandType.StoredProcedure;
insert.Parameters.AddWithValue("@Fullname", TextBox1.Text);
insert.Parameters.AddWithValue("@Address", TextBox2.Text);
insert.Parameters.AddWithValue("@Email", TextBox3.Text);

stormconn.Open();
insert.ExecuteNonQuery();
stormconn.Close();
if (IsPostBack)
{
TextBox1.Text = ("");
}
}
}
}
}

对不起,我为错误道歉,我认为您的问题中存在连接问题。我仔细阅读了代码,发现执行过程的代码有问题。我更新了代码。

protected void Button1_Click(object sender, EventArgs e)
{
using (var connection = new MySqlConnection("Server=tcp:xxxx.database.windows.net,1433;Initial Catalog=xxxx;Persist Security Info=False;User ID=xxxxx;Password=xxxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"))
{
MySqlCommand command = new MySqlCommand("InsertFullname", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new MySqlParameter("Fullname", TextBox1.Text));
command.Connection.Open();
var result = command.ExecuteNonQuery();
command.Connection.Close();
} 
}

最新更新