【问题标题】:Insert button is not inserting my data inside the database; what is missing? [duplicate]插入按钮没有在数据库中插入我的数据;缺什么? [复制]
【发布时间】:2014-01-28 16:28:35
【问题描述】:

我有以下内容,但没有插入数据:

protected void InsertButton_Click(object sender, EventArgs e)
{

    Program X = new Program();
    X.StudentName1 = NameTxt.Text;
    X.SudentAge1 = int.Parse(AgeTxt.Text);
    X.StudentID1 = int.Parse(IDTxt.Text);
    X.Insert();
}

这是我的插入方法

// Insert Method 
public void Insert()
{
    SqlConnection Connection = new SqlConnection(DBC.Constructor);
    string Sql = "insert into Details (StudentID,StudentName,SudentAge) Values (@StudentID1,@StudentName1,@SudentAge1)";
    SqlCommand Command = new SqlCommand(Sql, Connection);
    Command.Parameters.AddWithValue("@StudentID1", StudentID);
    Command.Parameters.AddWithValue("@StudentName1", StudentName);
    Command.Parameters.AddWithValue("@StudentAge1", SudentAge);


    try
    {
        Connection.Open();
        Command.ExecuteNonQuery();

        try
        {
            Console.WriteLine("Execute success");
        }

        catch
        {
            Console.WriteLine("Execute is not success");
        }

    }
    catch
    {
        Console.WriteLine("Error saving Student");
    }
    finally
    {
        try
        {

            Connection.Close();
        }
        catch
        {
        }
    }
}

【问题讨论】:

  • X.Insert(); 是什么?当您运行它时,究竟会发生什么?它会抛出异常吗?还是它什么都不做?您在哪里/如何检查是否没有插入数据? (一个常见的错误是误解了基于文件的数据库)
  • 首先在 Visual Studio 中设置一个断点,以找出代码的确切位置。变量设置是否正确?是否甚至调用了插入?如果所有这些都检查出来,您可以打印插入函数的代码吗?
  • 它根本没有做任何事情,我没有任何错误,先生,请帮助我
  • 运行此代码时遇到什么错误?
  • @user3192737 你的意思是Program X = new Program(); 上的断点永远不会被击中吗?您是否真的将InsertButton_Click 方法与按钮的Click 事件挂钩?

标签: c# asp.net sql


【解决方案1】:

这不是一个答案;它旨在显示相同的简化、更清晰和更健壮的编码,以帮助您找到问题:

protected void InsertButton_Click(object sender, EventArgs e)
{
   try
   {    
        var X = new Program(); // this line is a really good place for a
                               // breakpoint (press F9)
        X.StudentName1 = NameTxt.Text;
        X.SudentAge1 = int.Parse(AgeTxt.Text);
        X.StudentID1 = int.Parse(IDTxt.Text);
        X.Insert();
    }
    catch(Exception ex)
    {
        SomeMeaningfulAndWorkingExceptionDisplayMethod(ex);
    }
}

public void Insert()
{
    const string Sql = @"
    insert into Details (StudentID,StudentName,SudentAge)
    Values (@StudentID1,@StudentName1,@SudentAge1)";

    using(var conn = new SqlConnection(DBC.Constructor))
    using(var cmd = new SqlCommand(Sql, conn))
    {
        cmd.Parameters.AddWithValue("StudentID1", StudentID);
        cmd.Parameters.AddWithValue("StudentName1", StudentName);
        cmd.Parameters.AddWithValue("StudentAge1", SudentAge);
        conn.Open();
        cmd.ExecuteNonQuery();
    }
}

【讨论】:

    【解决方案2】:

    这在您的插入方法中吗?如果您使用的是 ADO.net,则必须执行您构建的命令,这与您在 cmets 中发布的内容不同。

                try
                {
                    SqlConnection Connection = new SqlConnection(DBC.Constructor);
                    string Sql = " Update Details Set Details = @StudentName1 , SudentAge1 = SudentAge1                            
                    Where StudentID1 = @StudentID1";
                    SqlCommand cmd = new SqlCommand(Sql, Connection);
                    cmd.Parameters.AddWithValue("StudentID1", StudentID); 
                    cmd.Parameters.AddWithValue("StudentName1", StudentName);
                    cmd.Parameters.AddWithValue("SudentAge1", SudentAge);
                    connection.Open();
                    command.ExecuteNonQuery();
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
    

    【讨论】:

    • 不,先生,我确实在另一个类中做了一个插入语句,我确实做了参数和每件事
    • SqlConnection Connection = new SqlConnection(DBC.Constructor); string Sql = "更新详细信息设置详细信息 = @StudentName1 , SudentAge1 = SudentAge1 其中 StudentID1 = @StudentID1"; SqlCommand cmd = new SqlCommand(Sql, Connection); cmd.Parameters.AddWithValue("StudentID1", StudentID); cmd.Parameters.AddWithValue("StudentName1", StudentName); cmd.Parameters.AddWithValue("SudentAge1", SudentAge);
    • 是在您的 Program 类上声明的插入语句吗?如果是这样,请粘贴 Insert 实现的代码,以便我们看到。
    • 从声明的开头粘贴整个插入方法并将其放入您的问题中。
    • 应该使用ExecuteNonQuery,而不是ExecuteReader...
    【解决方案3】:

    试试这段代码并检查你的数据库值是否与这个插入值匹配

      protected void InsertButton_Click(object sender, EventArgs e)
    {
    
    
    
        Insert(name.txt,convert.to.int16(AgeTxt.Text),convert.to.int16(IDTxt.Text));
    }
    
    
    // Insert Method 
     public void Insert(string name,int age,int studentid)
    {
              SqlConnection Connection = new SqlConnection(DBC.Constructor);
            string Sql = "insert into Details (StudentID,StudentName,SudentAge) Values    (@StudentID1,@StudentName1,@SudentAge1)";
              SqlCommand Command = new SqlCommand(Sql, Connection);
             Command.Parameters.AddWithValue("@StudentID1", studentid);
              Command.Parameters.AddWithValue("@StudentName1", name);
              Command.Parameters.AddWithValue("@StudentAge1", age);
    
    
         try
         {
        Connection.Open();
        Command.ExecuteNonQuery();
    
        try
        {
            Console.WriteLine("Execute success");
        }
    
        catch
        {
            Console.WriteLine("Execute is not success");
        }
    
    }
    catch
    {
        Console.WriteLine("Error saving Student");
    }
    finally
    {
        try
        {
    
            Connection.Close();
        }
        catch
        {
        }
    }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      • 2019-05-03
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 2019-11-23
      相关资源
      最近更新 更多