简体   繁体   English

从DLL执行SQL Server存储过程

[英]Execute a SQL Server stored procedure from a DLL

I want to execute a SQL Server stored procedure using C#. 我想使用C#执行SQL Server存储过程。 This is my current code: 这是我目前的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBCon;
using System.Data;
using System.Data.SqlClient;

    public class LoginDAl
    {
        public static DataSet  login_vald()
        {
            DBConnect myConnection = new DBConnect();
            SqlCommand comm = new SqlCommand("ph.validate_app_user", myConnection.connection);
            comm.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
        }

}

This is a dummy project for practice. 这是一个虚拟的练习项目。 Are DLLs the standard for DALs? DLL是DAL的标准吗? This is a desktop application. 这是一个桌面应用程序。 This a part of the DAL. 这是DAL的一部分。

The error is 错误是

login_vald()- not all code paths return a value login_vald() - 并非所有代码路径都返回一个值

The error is quite clear: 错误很明显:

login_vald()- not all code paths return a value login_vald() - 并非所有代码路径都返回一个值

...means that your function is missing a return statement with an object of type DataSet something like: ...意味着你的函数缺少一个带有DataSet类型对象的return语句,如:

public static DataSet  login_vald()
        {
            DBConnect myConnection = new DBConnect();
            SqlCommand comm = new SqlCommand("ph.validate_app_user", myConnection.connection);
            comm.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();

            return ds;   //this is missing
        }
  1. execute procedure ( something like da.Fill(ds); ) 执行程序( 像da.Fill(ds);
  2. return dataset (return ds;) 返回数据集 (返回ds;)

You have to look up for execute procedure command example 您必须查找执行过程命令示例

You need to have a return statement, you also need to fill the DataSet with records 您需要有一个return语句,还需要使用记录填充DataSet

        public static DataSet  login_vald()
        {
            DBConnect myConnection = new DBConnect();
            SqlCommand comm = new SqlCommand("ph.validate_app_user", myConnection.connection);
            comm.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(ds); //missing argument from SqlDataAdapter this will fill the ds

           return ds; //return filled DataSet
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM