1

私はテーブルを持っています

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
4

2 に答える 2

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 =@username", connection);       
  cmd.Parameters.Add( name, DbType.String ).Value = loginName;

  using( var reader = cmd.ExecuteReader() )
  {
    if (dr.Read())
    {
        // read your data here
    }
  }
}
于 2012-07-24T07:28:29.003 に答える
0

次の理由により、エラーが発生しています。

  • 引用符を使用していません - SQL 文字列定数は '' で囲む必要があります
  • あなたは間違っています:文字列を連結する代わりにパラメータを使用する必要があります
于 2012-07-24T07:31:58.560 に答える