我正在使用 c sharp 和 ms sql sever 2008 开发一个简单的数据库项目,但在编译程序时出现错误,它弹出此消息:
“StudentsInformationSystem.DB_conection”的类型初始化程序引发异常
我的代码:
namespace StudentsInformationSystem
{
class DB_Access
{
private SqlConnection conn;
public DB_Access()
{
conn = DB_conection.GetConnection(); //this is where i am getting the error on this line of code
}
public void add_student(string regNo,string fname, string lname, string phoneNo)
{
if (conn.State.ToString() == "closed")
{
conn.Open();
}
SqlCommand newCmd = conn.CreateCommand();
newCmd.Connection = conn;
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = "insert into student values('" + regNo + "','" + fname + "','" + lname + "','" + phoneNo + "')";
newCmd.ExecuteNonQuery();
}
}
}