1

文字列を返したいSQLテンプレートメソッドと、文字列を取得したいクエリを実行するさまざまなメソッドがあります。

private string sqlQueryReturnString(Action<SqlConnection> sqlMethod)
{
    string result = "";
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
    try
    {
        //open SQL connection
        conn.Open();
        result = sqlMethod(conn);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.Write(ex.ToString());
    }
    finally
    {
        conn.Close();
    }
    return result;
}

//Get the primary key of the currently logged in user
private string getPKofUserLoggedIn(SqlConnection conn)
{
    int result = 0;
    SqlCommand getPKofUserLoggedIn = new SqlCommand("SELECT [PK_User] FROM [User] WHERE [LoginName] = @userIdParam", conn);
    //create and assign parameters
    getPKofUserLoggedIn.Parameters.AddWithValue("@userIdParam", User.Identity.Name);
    //execute command and retrieve primary key from the above insert and assign to variable
    result = (int)getPKofUserLoggedIn.ExecuteScalar();
    return result.ToString();
}

上記は、これがどのようにアプローチされていると私が考えるかです。これは次の呼び出しです。

string test = sqlQueryReturnString(getPKofUserLoggedIn);

この部分は機能していません:

result = sqlMethod(conn);

sqlMethod は無効であると推定されているようです。

必要な機能を得るにはどうすればよいですか?

4

1 に答える 1

5

あなたが欲しいFunc<SqlConnection, string>。メソッド用、Action何かを返すメソッド用です。voidFunc

于 2013-07-18T15:37:36.967 に答える