返回C#中的对象

本文关键字:对象 返回 | 更新日期: 2023-09-27 18:07:56

我正试图在C#中返回一个对象。在JavaScript中,我会这样做:

function myFunction () {
 var myObj = { firstName: "John", lastName: "Smith", age: 20};
return myObj;
}

我读到的关于在C#中返回对象的所有内容都与此大不相同,所以这让我陷入了循环。

我想做的是对SQL运行一个查询,以获得一些用户信息,并返回用户角色、全名、电子邮件等。

这是我当前的C#代码:

    public static string getUserRole(string connectionString, string userId)
    {
        string role;
        SqlConnection sqlCon = new SqlConnection(connectionString);
        SqlCommand sqlCom = new SqlCommand();
        SqlDataReader reader;
        sqlCom.CommandText = "SELECT Role FROM myDatabase.Table WHERE Email = '" + userId + "'";
        sqlCom.CommandType = CommandType.Text;
        sqlCom.Connection = sqlCon;
        sqlCon.Open();
        reader = sqlCom.ExecuteReader();
        if (reader.Read())
        {
            role = reader.GetString(0);
            sqlCon.Close();
            return role;
        }
        else
        {
            return "An error has occurred";
        }
    }

我假设我需要做以下事情,但它不起作用:

    public static string getUserRole(string connectionString, string userId)
    {
        string role;
        string fullName;
        string email;
        SqlConnection sqlCon = new SqlConnection(connectionString);
        SqlCommand sqlCom = new SqlCommand();
        SqlDataReader reader;
        sqlCom.CommandText = "SELECT Role FROM myDatabase.Table WHERE Email = '" + userId + "'";
        sqlCom.CommandType = CommandType.Text;
        sqlCom.Connection = sqlCon;
        sqlCon.Open();
        reader = sqlCom.ExecuteReader();
        if (reader.Read())
        {
            public class myObject
            {
                role = reader.GetString(0);
                fullName = reader.GetString(1);
                email = reader.GetString(2);
            }
            sqlCon.Close();
            return myObject;
        }
        else
        {
            return "An error has occurred";
        }
    }

我可能还差得很远,但从我所读到的内容来看,c#中的对象基本上都是类。所以这是有道理的。我需要创建一个类来定义我的属性。这是正确的吗?我已经准备了很多帖子,并观看了一些youtube视频,但没有一个是"点击">

提前感谢您提供的任何有用的意见。

返回C#中的对象

public class UserInfo
{
    public string role;
        public string fullName;
        public string email;
    public string ErrorCode;
}

然后将签名更改为

public static UserInfo getUserRole(string connectionString, string userId)

然后更改

if (reader.Read())
        {
            public class myObject
            {
                role = reader.GetString(0);
                fullName = reader.GetString(1);
                email = reader.GetString(2);
            }
            sqlCon.Close();
            return myObject;
        }
        else
        {
            return "An error has occurred";
        }

创建一个UserInfo对象并返回。比如

UserInfo info = new UserInfo();
if (reader.Read())
        {
                info.role = reader.GetString(0);
                info.fullName = reader.GetString(1);
                info.email = reader.GetString(2);
            sqlCon.Close();
        }
        else
        {
            info.ErrorCode =  "An error has occurred";
        }
  return info;

注意:这不是最好的方法,但应该会让你继续前进。只是给你一个想法