【问题标题】:There's no error but data is not inserted to sql server table using ASP.NET?没有错误,但没有使用 ASP.NET 将数据插入到 sql server 表中?
【发布时间】:2020-05-19 18:23:40
【问题描述】:

插入查询没有给出任何错误,但数据没有插入到数据库中。当我在 sql server 中检查数据库时,似乎没有数据插入。我不确定代码的哪一部分有错误。

以下代码:

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;

namespace PayrollSystem05
{
    public partial class employee : System.Web.UI.Page
    {
      protected void Page_Load(object sender, EventArgs e)
        {

        }


protected void btnAdd_Click1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=DESKTOP-M0G68DT\\SQLEXPRESS;Initial Catalog=PRS;Integrated Security=True");
            SqlCommand cmd = new SqlCommand(@"INSERT INTO [payslip].[employee]
           ([emp_id]
           ,[emp_name]
           ,[emp_ic]
           ,[emp_address]
           ,[emp_mobile]
           ,[emp_email]
           ,[emp_startdate])
     VALUES
           ('"+ txtID.Text +"', '"+ txtName.Text +"', '"+ txtIC.Text +"', '"+ txtAdd.Text + "', '"+ txtMob.Text +"', '"+ txtEmail.Text +"', '" + txtStart.Text + "')", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Write("<script>alert('Employee added successfully.')</script>");
        }
    }
}```

【问题讨论】:

  • 能否请您检查一下您的数据库在 SQL Server 还是 SQL Server Express 中的位置?
  • 它在SQL Server中,只是发现SQL查询中有错误。
  • 使用存储过程而不是内联,这有助于轻松调试

标签: c# asp.net sql-server


【解决方案1】:

您可以将下面的代码与您的代码相同,我只是做了一些更改以更清晰易读。

var connectionString = @"Data Source=DESKTOP-M0G68DT\SQLEXPRESS;Initial Catalog=PRS;Integrated Security=True";
var queryString = @"INSERT INTO [payslip].[employee]
           (
              [emp_id]
             ,[emp_name]
             ,[emp_ic]
             ,[emp_address]
             ,[emp_mobile]
             ,[emp_email]
             ,[emp_startdate]
            )
          VALUES
            (
              @emp_id
             ,@emp_name
             ,@emp_ic
             ,@emp_address
             ,@emp_mobile
             ,@emp_email
             ,@emp_startdate
            )";

        using (SqlConnection sqlConnection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = sqlConnection.CreateCommand())
            {
                command.CommandText = queryString;
                command.Parameters.AddWithValue("@emp_id", txtID.Text);
                command.Parameters.AddWithValue("@emp_name", txtName.Text);
                command.Parameters.AddWithValue("@emp_ic", txtIC.Text);
                command.Parameters.AddWithValue("@emp_address", txtAdd.Text);
                command.Parameters.AddWithValue("@emp_mobile", txtMob.Text);
                command.Parameters.AddWithValue("@emp_email", txtEmail.Text);
                command.Parameters.AddWithValue("@emp_startdate", txtStart.Text);

                command.ExecuteNonQuery();
            } 
        }
        Response.Write("<script>alert('Employee added successfully.')</script>");

【讨论】:

    猜你喜欢
    • 2011-06-22
    • 1970-01-01
    • 2019-03-16
    • 2020-02-03
    • 2011-02-10
    • 2014-04-17
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    相关资源
    最近更新 更多