我在一个名为 SqlInteraction 的类中有一个方法:
public static string sqlQueryReturnString(Func<SqlConnection, string> 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;
}
这是使用它的不同类中的另一种方法:
private string getPKofUserLoggedIn(SqlConnection conn, string PointPerson)
{
int result = 0;
SqlCommand getPKofUserLoggedIn = new SqlCommand("SELECT [PK_User] FROM [User] WHERE [LoginName] = @userIdParam", conn);
//create and assign parameters
getPKofUserLoggedIn.Parameters.AddWithValue("@userIdParam", PointPerson);
//execute command and retrieve primary key from the above insert and assign to variable
result = (int)getPKofUserLoggedIn.ExecuteScalar();
return result.ToString();
}
我在第二种方法中传递字符串时遇到问题。如果我删除 'string PointPerson' 这个调用有效:
SqlInteraction.sqlQueryReturnString(getPKofUserLoggedIn);
我如何也传递字符串?