using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataBaseConnection;
using System.Data.SqlClient;
namespace Students
{
public class Program
{
// Filling The Fields
private int StudentID = 0;
private string StudentName = "";
private int SudentAge = 0;
private SqlConnection Connection = new SqlConnection();
// Properties
// Student ID
public int StudentID1
{
get { return StudentID; }
set { StudentID = value; }
}
// Student Name
public string StudentName1
{
get { return StudentName; }
set { StudentName = value; }
}
// SudentAge
public int SudentAge1
{
get { return SudentAge; }
set { SudentAge = value; }
}
// 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
{
}
}
}
这是我的按钮类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Students;
using System.Data.SqlClient;
using DataBaseConnection;
using System.Data;
public partial class SignUp : System.Web.UI.Page
{
public static string Constructor = "Data Source=FOUAD-PC;Initial Catalog=Students;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{
}
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();
}
}
catch(Exception x)
{
// change that
// Console.WriteLine("Execute is not success");
// with
lastError = "Execute is not success - reason:" + x.ToString();
}
and lastError是对象上的公共字符串,请在调用后检查它。
如果这是您的正确代码,您还有一些“空中”尝试/捕获,它们根本不起作用。