【发布时间】: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事件挂钩?