使用c#和ASP.NET编写数据并将其保存到SQL Server数据库

本文关键字:保存 SQL 数据库 Server 数据 ASP NET 使用 | 更新日期: 2023-09-27 18:04:06

我对c#很陌生,我想做的是让用户在字段中输入数据,当他们点击提交时,它会写入数据库。我真的卡住了,如果你能帮助我,我将不胜感激。

这是我到目前为止写的。

我的标记:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Registration.aspx.cs" Inherits="subscribe_Registration" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Your Earth, Your Home.</title>
<link rel="stylesheet" type="text/css" href="../StyleSheet.css" />
</head>
<body>
<form id="form1" runat="server">
<div class="container">
    <header>
        <img src="../logo.jpg" height="200" />
    </header>
        <nav>
        <ul>
            <li><a href="Default.aspx">Home</a></li>
            <li><a href="about-us.aspx">About Us</a></li>
            <li><a href="gallery.aspx">Image Gallery</a></li>
            <li><a href="http://your-earth-yourhome.blogspot.com.au/">Blog</a></li>
            <li><a href="subscribe/registration.aspx">Subscribe</a></li>
            <li><a href="calendar.aspx">Calendar</a></li>
            <li><a href="donate.aspx">Donate</a></li>
            <li><a href="shop.aspx">Shop</a></li>
        </ul>
    </nav>
    <section>
        <h1>Subscribe</h1>
        <div class="div1">
            <br />
            &nbsp;&nbsp;&nbsp;
            Name:
            <br />
            &nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="txtName" runat="server" Width="200" Height="20"></asp:TextBox>
            <br />
            <br />
            &nbsp;&nbsp;&nbsp;
            Age:<br />
            &nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="txtAge" runat="server" Width="200" Height="20">     </asp:TextBox>
            <br />
            <br />
            &nbsp;&nbsp;&nbsp;
            Sex:<br />
            &nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="txtSex" runat="server" Width="200" Height="20"></asp:TextBox>
            <br />
            <br />
        </div>
        <div class="div2">
            &nbsp;&nbsp;&nbsp;
            Address:<br />
            &nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="txtAddress" runat="server" Width="200" Height="20"></asp:TextBox>
            <br />
            <br />
            &nbsp;&nbsp;&nbsp;
            Email Address:<br />
            &nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="txtEmail" runat="server" Width="200" Height="20"></asp:TextBox>
            <br />
            <br />
            &nbsp;&nbsp;&nbsp;
            Phone Number:<br />
            &nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="txtPhone" runat="server" Width="200" Height="20"></asp:TextBox>
            <br />
            <br />
            &nbsp;&nbsp;&nbsp;
            <asp:Button ID="Button1" runat="server" Text="Subscribe" Width="200" Height="30" OnClick="Button1_Click" />
            <asp:PlaceHolder ID="phSuccess" runat="server" Visible="false">
                <div id="success">
                    <p>Thank you for your registration!</p>
                </div>
            </asp:PlaceHolder>
    </section>
    <aside>
        <div class="links">
            <h3>Here are our affiliates:</h3>
            <ul>
                <li><a href="#">http://www.earthhour.org</a></li>
                <li><a href="#">http://www.50waystohelp.com</a></li>
                <li><a href="#">http://www.greenpeace.org</a></li>
                <li><a href="#">http://www.wwf.org.au/</a></li>
            </ul>
        </div>
    </aside>
    <footer>&copy; Copyright Your Earth, Your Home. All Rights Reserved</footer>
</div>
</form>
</body>
</html>

My c# code

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;
using System.Configuration;
using System.Data;
public partial class subscribe_Registration : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        Registration.Registration reg = new Registration.Registration();
        reg.Name = txtName.Text;
        reg.Age = Int32.Parse(txtAge.Text);
        reg.Sex = txtSex.Text;
        reg.Address = txtAddress.Text;
        reg.Email = txtEmail.Text;
        reg.Phone = Int32.Parse(txtPhone.Text);
        phSuccess.Visible = true;
    }
    public void InsertRegistration()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString);
        SqlCommand cmd = new SqlCommand("dbo.Procedure", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter parameterName = new SqlParameter("@Name", SqlDbType.VarChar, 50);
        SqlParameter parameterAge = new SqlParameter("@Age", SqlDbType.VarChar, 50);
        SqlParameter parameterSex = new SqlParameter("@Sex", SqlDbType.VarChar, 50);
        SqlParameter parameterAddress = new SqlParameter("@Address", SqlDbType.VarChar, 50);
        SqlParameter parameterEmail = new SqlParameter("@Email", SqlDbType.VarChar, 100);
        SqlParameter parameterPhone = new SqlParameter("@Phone", SqlDbType.VarChar, 100);
        parameterName.Value = txtName;
        parameterAge.Value = txtAge;
        parameterEmail.Value = txtSex;
        parameterAddress.Value = txtAddress;
        parameterEmail.Value = txtEmail;
        parameterPhone.Value = txtPhone;
        cmd.Parameters.Add(parameterName);
        cmd.Parameters.Add(parameterAge);
        cmd.Parameters.Add(parameterSex);
        cmd.Parameters.Add(parameterAddress);
        cmd.Parameters.Add(parameterEmail);
        cmd.Parameters.Add(parameterPhone);
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }
        finally
        {
            cmd.Dispose();
            conn.Close();
        }
    }
}

我也有一个类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace Registration
{
    public class Registration
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Sex { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public int Phone { get; set; }
    }
}
我真的被困住了,我不确定从这里该去哪里。我想将用户数据保存到数据库中。不知道什么是最好的方法,也不知道从哪里开始。如果你能给我指出正确的方向,我将非常感激。

提前感谢:)

使用c#和ASP.NET编写数据并将其保存到SQL Server数据库

1。在SQL中创建一个Users表,用于存储用户数据,并将一个存储过程插入到该表中:

CREATE PROCEDURE [dbo].[Procedure]
@Name VARCHAR(50),
@Age VARCHAR(50),
@Sex VARCHAR(50),
@Address VARCHAR(50),
@Email VARCHAR(100),
@Phone VARCHAR(100)
AS
BEGIN
    SET NOCOUNT ON;
    INSERT INTO Users(Name,Age,Sex,[Address],Email,Phone)
    VALUES(@Name,@Age,@Sex,@Address,@Email,@Phone)
END
GO

2。像这样修改文件后面的代码:

  protected void Button1_Click(object sender, EventArgs e)
    {
        var reg = new Registration.Registration
        {
            Name = txtName.Text,
            Age = Int32.Parse(txtAge.Text),
            Sex = txtSex.Text,
            Address = txtAddress.Text,
            Email = txtEmail.Text,
            Phone = Int32.Parse(txtPhone.Text)
        };
        this.InsertRegistration(reg);
        phSuccess.Visible = true;
    }
    public void InsertRegistration(Registration.Registration reg)
    {
        try
        {
            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString))
            {
                using (var cmd = new SqlCommand("dbo.Procedure", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    var parameterName = new SqlParameter("@Name", SqlDbType.VarChar, 50);
                    var parameterAge = new SqlParameter("@Age", SqlDbType.VarChar, 50);
                    var parameterSex = new SqlParameter("@Sex", SqlDbType.VarChar, 50);
                    var parameterAddress = new SqlParameter("@Address", SqlDbType.VarChar, 50);
                    var parameterEmail = new SqlParameter("@Email", SqlDbType.VarChar, 100);
                    var parameterPhone = new SqlParameter("@Phone", SqlDbType.VarChar, 100);
                    parameterName.Value = reg.Sex;
                    parameterAge.Value = reg.Age;
                    parameterSex.Value = reg.Email;
                    parameterAddress.Value = reg.Address;
                    parameterEmail.Value = reg.Email;
                    parameterPhone.Value = reg.Phone;
                    cmd.Parameters.Add(parameterName);
                    cmd.Parameters.Add(parameterAge);
                    cmd.Parameters.Add(parameterSex);
                    cmd.Parameters.Add(parameterAddress);
                    cmd.Parameters.Add(parameterEmail);
                    cmd.Parameters.Add(parameterPhone);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

你应该在你的。cs代码中使用断点。在这类程序中最常见的错误是数据库或。cs文件中的字段名称略有不同,因为服务器没有获得数据库中每个字段的值。这给出一个错误。

所以适当的使用断点和调试器来调试你的程序,让你发现在哪个点你得到了一个错误。