想要实现的目标:我正在尝试从 sql 存储过程中收集单行数据,并将其DataTable
放入项目中单独 cs 文件的类中。
然后将它(作为 DataTable 对象返回到项目的另一部分(cs 页面)上的调用代码。
然后我希望从所述行中获取每一列的单元格值,DataTable
并将其转换string
为将成为session variable
.
由于某些我不明白的原因(我仍然在编码中咬牙切齿),我无法达到我所期望的单元格值。当我在VS2010
“ Locals
”选项卡中检查它们时,我看到预期的数据已经拉到“dtDerverVars >Rows > Non-Public Members > Results View > [0] > ItemArray”,我可以在那里看到预期的值。
但是使用检索单元数据的常用方法,我在 tinternet 上找到了无数示例,例如:
DataTable Dt = GetUserDetails();
string username = Dt.Rows[0]["username"].ToString();
似乎没有按预期工作。
请查看我尝试使用的代码,该代码存储在公共类“ clCommonFunctions
”中的单独 cs 文件中:
public static DataTable TryAutoLogin(string strREMOTE_USER)
{
SqlConnection siConnection = new SqlConnection();
siConnection.ConnectionString = Databases.getDbConnectionString("csSharedInfo");
siConnection.Open();
SqlCommand seCmd = new SqlCommand("GetSignOnDetails", siConnection);
seCmd.CommandType = CommandType.StoredProcedure;
seCmd.Parameters.Add(new SqlParameter("@DomainAccount", SqlDbType.NVarChar, 300));
seCmd.Parameters["@DomainAccount"].Value = strREMOTE_USER;
seCmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter();
seCmd.Connection = siConnection;
sda.SelectCommand = seCmd;
DataTable dtServerVars = new DataTable();
sda.Fill(dtServerVars);
siConnection.Close();
if (dtServerVars != null)
{
if (dtServerVars.Rows.Count > 0)
{
return dtServerVars;
}
}
return null;
}
这是来自同一项目中名为“Site Master.cs”的cs页面的调用代码:
DataTable dtServerVars = clCommonFunctions.TryAutoLogin("europe\\MrTest");
Session["CablingUserID"]= dtServerVars.Rows[0]["CablingUserID"].ToString();
Session["CablingUseremail"] = dtServerVars.Rows[0]["CablingUseremail"].ToString();
Session["CablingLogin"] = dtServerVars.Rows[0]["CablingLogin"].ToString();
Session["CablingPassword"] = dtServerVars.Rows[0]["CablingPassword"].ToString();
Session["CablingPersonnel"] = dtServerVars.Rows[0]["CablingPersonnel"].ToString();
Session["CablingSurname"] = dtServerVars.Rows[0]["CablingSurname"].ToString();
Session["CablingFirstName"] = dtServerVars.Rows[0]["CablingFirstName"].ToString();
Session["CablingSuperUser"] = dtServerVars.Rows[0]["CablingSuperUser"].ToString();
Session["CablingDateAdded"] = dtServerVars.Rows[0]["CablingDateAdded"].ToString();
Session["CablingContact"] = dtServerVars.Rows[0]["CablingContact"].ToString();
Session["CablingApprovalAuthority"] = dtServerVars.Rows[0]["CablingApprovalAuthority"].ToString();
Session["CablingAdminUser"] = dtServerVars.Rows[0]["CablingAdminUser"].ToString();
Session["SharedInfoID"] = dtServerVars.Rows[0]["SharedInfoID"].ToString();
Session["SharedInfousername"] = dtServerVars.Rows[0]["SharedInfousername"].ToString();
Session["SharedInfopassword"] = dtServerVars.Rows[0]["SharedInfopassword"].ToString();
Session["SharedInfoname"] = dtServerVars.Rows[0]["SharedInfoname"].ToString();
Session["SharedInfoemail"] = dtServerVars.Rows[0]["SharedInfoemail"].ToString();
Session["SharedInfoICLlocation"] = dtServerVars.Rows[0]["SharedInfoID"].ToString();
Session["SharedInfoPhone"] = dtServerVars.Rows[0]["SharedInfoPhone"].ToString();
Session["SharedInfoSecLevel"] = dtServerVars.Rows[0]["SharedInfoSecLevel"].ToString();
Session["IMSUserID"] = dtServerVars.Rows[0]["IMSUserID"].ToString();
Session["IMSUserName"] = dtServerVars.Rows[0]["IMSUserName"].ToString();
Session["IMSIsAnonymous"] = dtServerVars.Rows[0]["IMSIsAnonymous"].ToString();
Session["IMSLastActivityDate"] = dtServerVars.Rows[0]["IMSLastActivityDate"].ToString();
Session["loggedin"] = "unknown";
请对此提供一些指导...