cmd.ExecuteNonQuery(); SqlException未处理

时间:2018-02-04 03:52:21

标签: c#

每次单击没有值的按钮(null或空白)时,都会出错。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace Fifth_Attempt
{
    class Prof
    {
        public SqlException AddProfessorInformation(ProfModel oPM)

        {
            string connStr = @"Data Source=USER-PC;Integrated Security=True;";
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand("INSERT INTO ProfInfo(ID,FirstName,MiddleName,LastName,Department,Username,Password)  VALUES(@LRN,@FirstName,@MiddleName,@LastName,@Department,@Username,@Password)", conn);
            cmd.Parameters.AddWithValue("@ID", oPM.ID);
            cmd.Parameters.AddWithValue("@Firstname", oPM.FirstName);
            cmd.Parameters.AddWithValue("@MiddleName", oPM.MiddleName);
            cmd.Parameters.AddWithValue("@LastName", oPM.LastName);
            cmd.Parameters.AddWithValue("@Department", oPM.Department);
            cmd.Parameters.AddWithValue("@Username", oPM.Username);
            cmd.Parameters.AddWithValue("@Password", oPM.Password);
            conn.Open();
            cmd.ExecuteNonQuery();
            return null;         

        }
    }
}

>link to full code

为什么我会收到错误?

1 个答案:

答案 0 :(得分:1)

为什么为@LRN添加参数缺失?

cmd.Parameters.AddWithValue("@LRN", ??);