2012-07-24 85 views
1

我有表如何获取用户名到MVC3中的资源名称?

Resource 

ResourceId | ResourceName |username | password  
    1  | raghu  | aaaa | ****** 
    2  | anil  | bbbb | ******   

BugHistory 

BugHisoryId | FixedByID | AssignedByID 
    1  | 2  | 1 
    2  | 1  | 2 

卫生组织的登录名同名的用户名来获取资源名称。 FixedByIdforeign key(FixedById) reference Resource(ResourceId)

我控制器代码

public ActionResult BugHistory(BugTracker_DataHelper bugdatahelepr, string loginname, string EmployeName) 
{ 
    Session["UserName"] = "aaaa"; 
    loginname = Session["UserName"].ToString(); 
    //bugdatahelepr.Username = loginname.ToString(); 
    //var username = bugdatahelepr.Username; 

    SqlConnection connection = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MvcBugTracker;Data Source=SSDEV6\SQLEXPRESS"); 
    connection.Open(); 
    SqlCommand cmd = new SqlCommand("select ResourceName from Resources where UserName =" + loginname, connection);  

    SqlDataReader dr = cmd.ExecuteReader(); 

    if (dr.Read()) 
    { 
     bugdatahelepr.FixedByID = Convert.ToInt16(dr["ResourceName"]); 
     //updatemodel.ProjectId = Convert.ToInt16(dr["ProjectId"]); 
    } 
    else 
    { 
     dr.Close(); 
    } 
    dr.Close(); 
    connection.Close(); 

    //ViewBag.BugHistoryId = new SelectList(ToStatusDropdown(), "BugHistoryId", "ToStatus"); 
    //ViewData.AssignedToID=new SelectList() 

    return View(); 
} 

我的视图代码

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Gridview_BugTracker.Models.BugTracker_DataHelper>" %> 

<!DOCTYPE html> 

<html> 
<head runat="server"> 
    <title>BugHistory</title> 
</head> 
<body> 
    <div> 

    <%: ViewBag.Title = "BugHistory"%> 
    <% using (Html.BeginForm()) 
    { %>  
     <%:Html.ValidationSummary(true)%>  

     <fieldset> 
     <legend>BugHistory</legend> 

     <div class="editor-label"> 
      <%:Html.LabelFor(model => model.FixedByID)%> 
     </div> 
     <div class="editor-field"> 
      <%:Html.LabelFor(model => model.FixedByID)%> 
      <%:Html.ValidationMessageFor(model => model.FixedByID)%> 
     </div> 
     <div class="editor-label"> 
     <%:Html.LabelFor(model => Model.Resolution)%> 
    </div> 
    <div class="editor-field"> 
     <%:Html.EditorFor(model => model.Resolution)%> 
     <%:Html.ValidationMessageFor(model => model.Resolution)%> 
    </div> 
     <%: Html.DropDownList("BugHistoryId", (SelectList)ViewBag.BugHistoryId, "--Select Project--")%> 
     <%: Html.ValidationMessage("BugHistoryId")%> 
     </fieldset> 

     <% }%> 
     <form action="AssignProject.aspx" method="post"> 
      <p> <input type="submit" value="insert" /></p> 
      </form>   


    </div> 
</body> 
</html> 

我得到错误

Invalid column name 'aaaa'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'raghu'. 

Source Error: 


Line 270:   SqlCommand cmd = new SqlCommand("select ResourceName from Resources where UserName =" + loginname, connection);  
Line 271:   
Line 272:   SqlDataReader dr = cmd.ExecuteReader(); 
Line 273:    
Line 274:   if (dr.Read()) 


Source File:  C:\Raghu\Gridview_BugTracker\Gridview_BugTracker\Controllers\ProjectsController.cs Line: 272 

当我登录页面用户名aaaa获取资源名称。任何人都可以帮我做到这一点?

IN veiw page i Diplay like this 

FixedBYID -----------raghu <---Lable in disabale 

AssignedBY ID-------- anil <----dropdownlist in disable 
+0

从错误中看,问题是它试图在数据库中找到列“aaaa”,而不是列“username”。 – 2012-07-24 07:23:59

+0

@GarrettFogerlie ...这是我的查询“从资源中选择ResourceName,其中UserName =”+ loginname, – 2012-07-24 07:25:15

+0

@GarrettFogerlie ..我在数据库中获得用户名相同,即使得到这种类型的错误来了......我该怎么做.. – 2012-07-24 07:28:40

回答

2

在这段代码中你应该改进一些东西。首先,总是(并且我的意思是总是)在using声明中包装连接和数据收集器。

二,做不是连接字符串来建立SQL查询。这会为您打开SQL插入攻击。相反,你应该使用参数。

现在,对于您所看到的错误。您只需追加的用户名查询,这将使您的查询是这样的:

select ResourceName from Resources where UserName =aaaa 

用户名没有报价,这意味着数据库服务器将尝试寻找一个名为“AAAA”列。这正是你所看到的错误信息。

数据访问代码应该是这个样子:

using(var connection = new SqlConnection(@".... your connection string here ...")) 
{ 
    connection.Open(); 

    var cmd = new SqlCommand("select ResourceName from Resources where UserName [email protected]", connection);  
    cmd.Parameters.Add(name, DbType.String).Value = loginName; 

    using(var reader = cmd.ExecuteReader()) 
    { 
    if (dr.Read()) 
    { 
     // read your data here 
    } 
    } 
} 
+0

谢谢你很多...我得到了输出... – 2012-07-24 08:08:30

0

你,因为遇到了错误:

  • 你不使用引号 - 在SQL字符串常量必须''
  • 你做错了:你应该使用参数,而不是连接字符串