插入按钮不是在数据库中插入我的数据;缺什么?

时间:2014-01-28 07:39:04

标签: c# asp.net sql

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

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
        {
        }
    }
}

3 个答案:

答案 0 :(得分:2)

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

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();
    }
}

答案 1 :(得分:0)

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

            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);
            }

答案 2 :(得分:0)

尝试此代码并检查您的数据库值是否与此插入值匹配

  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
    {
    }
}
 }