2011-03-21 7 views
0

을 sqlsyntax합니다 :문자열 내가 가진 문자열

string theUserId = Session["UserID"].ToString(); 

을하지만

{ 
     if (Session["UserID"] != null) 
     { 
      string theUserId = Session["UserID"].ToString(); 
      Label1.Text = Convert.ToString(theUserId); 


     OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite; User=x; Password=x;"); 
     cn.Open(); 
     OdbcCommand cmd = new OdbcCommand("SELECT User.FirstName, User.SecondName, User.Aboutme, User.DOB, Pictures.picturepath FROM User LEFT JOIN Pictures ON User.UserID = Pictures.UserID WHERE [email protected]"), cn); 

     cmd.Parameters.AddWithValue("@UserID", theUserId); 

     OdbcDataReader reader = cmd.ExecuteReader(); 
     while (reader.Read()) 
     { 
      Name.Text = String.Format("{0} {1}", reader.GetString(0), reader.GetString(1)); 
      Aboutme.Text = String.Format("{0}", reader.GetString(2)); 
      Age.Text = String.Format("{0}", reader.GetString(3)); 
      Image1.ImageUrl = String.Format("{0}", reader.GetString(4)); 
     } 


    } 
} 
} 

User.UserID=1가 어떻게 바꿀 것이 sqlsnytax에 문자열을 추가하는 방법을 잘 모릅니다 무언가에

+1

@Brandon에는 요구 사항에 따라 올바른 답이 있습니다. 그러나 몇 가지 유의해야합니다. 첫째, "theUserId"는 실행 전에 소생 될 필요가 있습니다. 그것이 의미하는 바에는 SQL 인젝션이 그곳에 쓰여져 있습니다. 둘째, 나는 당신이 이것을 지나가고 있다는 것이 확실하지 않지만, 누군가 그것을 찢어야 할 것처럼 보입니다. codereview.stackexchange.com – NotMe

+0

@ uhhardy에서 코드 일부를 게시 할 수 있습니다. WraithNath는 기본적으로 모든 잡음이없는 동일한 대답을 가지고 있습니다. 나는 내 것을 제거하고 대신 자신을 upvoted. – Brandon

+0

고맙다 Brandon, 나는 따라갈 것이다. – ukhardy

답변

4

은 다음을 참조하십시오를 concatinating입니다 @parameter 이름을 사용하여 매개 변수를 정의하고 .Parameters.AddWithValue

를 사용하여 추가 할 수 있습니다. 가장 중요한 것은 연결을 정리하는 USING 절입니다. 어느 쪽이든 이것을 사용하거나 시도에서 모든 것을 포장해야합니다. 적절한 처분 요청을합니다.

if (Session["UserID"] != null) 
{ 
    string theUserId = Session["UserID"].ToString(); 
    Label1.Text = Convert.ToString(theUserId); 

    using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite; User=root; Password=commando;")) { 
     cn.Open(); 
     using (OdbcCommand cmd = new OdbcCommand("SELECT User.FirstName, User.SecondName, User.Aboutme, User.DOB, Pictures.picturepath FROM User LEFT JOIN Pictures ON User.UserID = Pictures.UserID WHERE [email protected]", cn)) { 

      cmd.Parameters.AddWithValue("@UserID", theUserId); 

      using (OdbcDataReader reader = cmd.ExecuteReader()) { 
       while (reader.Read()) 
       { 
        Name.Text = String.Format("{0} {1}", reader.GetString(0), reader.GetString(1)); 
        Aboutme.Text = String.Format("{0}", reader.GetString(2)); 
        Age.Text = String.Format("{0}", reader.GetString(3)); 
        Image1.ImageUrl = String.Format("{0}", reader.GetString(4)); 
       } 
      } // using reader 
     } // using cmd 
    } // using connection 
} 
0
User.UserID="theUserId" 같은
WHERE User.UserID = $UserID 

그런 다음 '$ UserID'라는 매개 변수를 사용중인 Command 개체에 추가하면 쿼리를 실행할 때 값을 선택합니다. 난 당신이 MySQL을 위해 사용중인 드라이버 확실하지 않다

주, 나는 매개 변수 $로 시작해야합니다 생각 ,하지만 난 100 % 확실하지 않다. SQL Server에서는 @입니다.

+0

이것은 .net이 아니라 ... – NotMe

+0

질문은 MySQL로 태그가 붙어 있습니다. 이전에 사용했던 .NET 용 관리 MySQL 드라이버가 있습니다. 포스터가 MySQL을 사용한다고 가정했습니다. – kprobst

+1

참. 그러나 $ UserId 표기법은 PHP 특정 및 MySql 구문이 아닙니다. MySql은 다른 RDBM이 사용하는 것과 동일한 변수 구문을 사용합니다. 예를 들면 다음과 같습니다 : @UserId 좀 더 자세한 정보를 원하시면 http://dev.mysql.com/doc/refman/5.0/en/user-variables.html을보십시오 – NotMe

0

이와 비슷한 제품을 찾고 계십니까?

string.Format ("SELECT User.FirstName, User.Aboutme, User.Aboutme, User.DOB, Pictures.picturepath 사용자 LEFT JOIN 그림에서 User.UserID = Pictures.UserID WHERE User.UserID = {0} ", theUserId);

1
string theUserId = Session[ "UserID" ].ToString(); 

OdbcCommand cmd = new OdbcCommand(
     "SELECT User.FirstName, User.SecondName, User.Aboutme, User.DOB, Pictures.picturepath FROM User LEFT JOIN Pictures ON User.UserID = Pictures.UserID WHERE [email protected]" 
    ), cn); 

    cmd.Parameters.AddWithValue("@UserID", theUserId); 

당신이 훨씬 및 String.format보다 안전 또는 문자열 자신

+0

메신저에 오류가 발생했습니다. cn); 이게 이상했기 때문에 정말 이상 했어. 내가 끈으로 묶어서 추가 했어?나는 다시 편집하여 –

+0

@ Garrith, 아니, 그것은 작동하지 않았다. 게시 한 샘플에는 문자열 뒤에 추가로')'이 있습니다. 그것을 제거하십시오. – Brandon