无法使用ADO.net在C#中插入和查看数据

无法使用ADO.net在C#中插入和查看数据,c#,class,ado.net,C#,Class,Ado.net,我是C#的新手。我正在尝试使用ADO.Net创建一个简单的应用程序,我只想在其中插入和查看员工详细信息。我无法插入和查看员工的详细信息。我要调用的方法是add_employee(),它存在于EmployeeDAL类中 namespace Employee { class EmployeeDAL { public void add_employee(EmployeeBO emp) { string connectionStr

我是C#的新手。我正在尝试使用ADO.Net创建一个简单的应用程序,我只想在其中插入和查看员工详细信息。我无法插入和查看员工的详细信息。我要调用的方法是add_employee(),它存在于EmployeeDAL类中

namespace Employee
{
    class EmployeeDAL
    { 
        public void add_employee(EmployeeBO emp)
        {
            string connectionString = "Data Source=SQLEXPRESS01;Initial Catalog=abc";
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "sp_insert";
            command.Connection = con;
            command.Parameters.AddWithValue("@empname", emp.EmployeeName);
            command.Parameters.AddWithValue("@deptid", emp.DepatNumber);
            con.Open();
            int result = command.ExecuteNonQuery();
            if (result > 0)
                Console.WriteLine("Record inserted successfully");
            else
                Console.WriteLine("Some Error Occured");
            con.Close();
           //return result;
        }
    }
}
我的主要方法是这样

    using System;
using System.Collections.Generic;



namespace Employee
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Employee Management system");
            Console.WriteLine("Menu\n");
            Console.WriteLine("1.Add Employees\n");
            Console.WriteLine("2.View Employees\n");
            Console.WriteLine("Enter your choice\n");
            int ch = Convert.ToInt32(Console.ReadLine());
            EmployeeBO emp = new EmployeeBO();
            EmployeeDAL dal = new EmployeeDAL();

            switch(ch)
            {
                case 1:

                    Console.WriteLine("Enter the name of the employee");
                    string name = Console.ReadLine().ToString();
                    emp.EmployeeName = name;
                    Console.WriteLine("Enter the department of the employee");
                    int deptNo = Convert.ToInt32(Console.ReadLine());
                    emp.DepatNumber = deptNo;

                     dal.add_employee(emp);
                    break;

我无法插入和查看已存在的数据

我的EmployeeBO类只包含Employee类

我的sp_插入存储过程是

create proc sp_insert(
@empname varchar(20),
@deptid int
)
as begin
insert into employee(empName,deptno) values(@empname,@deptid)
end
这就是我创建表的方式

create table employee( empno int identity(1000,1) primary key , empName varchar(20)) 
alter table employee add deptno int 

create table department(deptno int identity(1,1) primary key, deptName varchar(20))


alter table employee add constraint fk1 foreign key(deptno) references department(deptno) 

作为@mjwills,我认为这是一个运行未编译代码的案例

例如,如果您运行代码(F5),但存在编译错误,并且选择运行编译的最后一个版本,则可能会发生这种情况。(还有其他情况可能导致运行编辑器中显示的代码)

一种解决方案是添加一个更改,允许您确认正在运行新代码,重新编译并再次运行


您还可以在代码中放入一些未编译的内容,并确认运行/调试失败。

class EmployeeDAL应该是公共的。错误消息是什么?您是否尝试调试此代码?当您输入上面的最后一个代码时,您确定变量ch保持值1吗?你看到输入姓名和部门号的请求了吗?我的员工姓名是“uma”,部门id是2。My department表包含1、2和3个部门id的条目。因此不可能存在外键冲突econ.Open();是调试器命中的最后一行
string connectionString=“Data Source=SQLEXPRESS01;Initial Catalog=abc”看起来可能不完整。运行代码时,将在
con.Open
上引发异常。例外说明了什么?