使用C#

时间:2018-11-20 00:33:44

标签: c# sql sql-server stored-procedures

我第一次使用C#和SQL Server构建简单的ERP系统。我正在尝试检查textBox中的值是否已存在于数据库中;当表单启动并输入字段时,我会遇到异常

这里是检查功能:

    private void txtRef_Validated(object sender, EventArgs e)
    {          
        BL.CLS_PRODUCTS prd = new BL.CLS_PRODUCTS();
        DataTable Dt = new DataTable();
        Dt = prd.VarifyProductID(txtRef.Text);

        if (Dt.Rows.Count > 0)
        {
            MetroMessageBox.Show(this, "الصنف موجود مسبقاً", "تنبيه", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            txtRef.Focus();
            txtRef.SelectionStart = 0;
            txtRef.SelectionLength = txtRef.Text.Length;
        }
    }

结尾的数据表
da.Fill(dt);

public DataTable SelectData(string stored_procedure, SqlParameter[] param)
{
        SqlCommand sqlcmd = new SqlCommand();
        sqlcmd.CommandType = CommandType.StoredProcedure;
        sqlcmd.CommandText = stored_procedure;
        sqlcmd.Connection = sqlconnection;

        if (param != null)
        {
            for (int i = 0; i < param.Length; i++)
            {
                sqlcmd.Parameters.Add(param[i]);
            }
        }

        SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }

这是我的验证检查功能:

public DataTable VerifyProductID(string ID)
{
    DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
    DataTable Dt = new DataTable();

    SqlParameter[] param = new SqlParameter[1];
    param[0] = new SqlParameter("@ID", SqlDbType.VarChar, 50);
    param[0].Value = ID;

    Dt = DAL.SelectData("VerifyProductID", null);
    DAL.Close();

    return Dt;
}

SQL Server存储过程:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROC [dbo].[VerifyProductID]
    @ID VARCHAR(50)
AS
    SELECT * 
    FROM PRODUCTS 
    WHERE ID = @ID

例外:

  

System.Data.SqlClient.SqlException未通过用户代码处理
  HResult = -2146232060
  Message =过程或函数'VerifyProductID'需要未提供的参数'@ID'。
  Source = .Net SqlClient数据提供程序
  ErrorCode = -2146232060
  课= 16
  LineNumber = 0
  数= 201
  Procedure = VerifyProductID
  服务器=。\ SQLEXPRESS
  状态= 4

     

StackTrace:
  在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔值breakConnection,操作1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction)              在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔调用方HasConnectionLock,布尔asyncClose)              在System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean和dataReady)              在System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()              在System.Data.SqlClient.SqlDataReader.get_MetaData()              在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,字符串resetOptionsString)              在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean异步,Int32超时,Task&task,Boolean asyncWrite,SqlDataReader ds)              在System.Data.SqlClient.SqlCommand.RunExecuteReader处(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔返回流,字符串方法,TaskCompletionSource`1完成,Int32超时,任务和任务,布尔asyncWrite)              在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String方法)              在System.Data.SqlClient.SqlCommand.ExecuteReader上(CommandBehavior行为,String方法)              在System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior行为)              在System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior行为)              在System.Data.Common.DbDataAdapter.FillInternal(DataSet数据集,DataTable []数据表,Int32 startRecord,Int32 maxRecords,字符串srcTable,IDbCommand命令,CommandBehavior行为)              在System.Data.Common.DbDataAdapter.Fill处(DataTable [] dataTables,Int32 startRecord,Int32 maxRecords,IDbCommand命令,CommandBehavior行为)              在System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)              在c:\ Users \ kh \ Documents \ Visual Studio 2012 \ Projects \ E_AND_M \ E_AND_M \ DAL \ DataAccessLayer.cs:line 58中的E_AND_M.DAL.DataAccessLayer.SelectData(字符串stored_procedure,SqlParameter []参数)              在c:\ Users \ kh \ Documents \ Visual Studio 2012 \ Projects \ E_AND_M \ E_AND_M \ BL \ CLS_PRODUCTS.cs:line 49中的E_AND_M.BL.CLS_PRODUCTS.VarifyProductID(字符串ID)              在c:\ Users \ kh \ Documents \ Visual Studio 2012 \ Projects \ E_AND_M \ E_AND_M \ PL \ FRM_ADD_PROUDECT.cs:第33行中的E_AND_M.PL.FRM_ADD_PROUDECT.txtRef_Validated(对象发送者,EventArgs e)              在System.Windows.Forms.Control.OnValidated(EventArgs e)              在System.Windows.Forms.Control.PerformControlValidation(Boolean bulkValidation)              在System.Windows.Forms.ContainerControl.ValidateThroughAncestor(控件ancestorControl,布尔preventFocusChangeOnError)         InnerException:

1 个答案:

答案 0 :(得分:1)

Dt = DAL.SelectData("VarifyProductID", null);

应该是

Dt = DAL.SelectData("VarifyProductID", param);