2013-01-07 1 views
1

mysql 2008 데이터베이스에서 필드를 가져 오려고합니다. 나는 (내가 전에 몇 번 사용했던)이 작은 코드를 작성 - 그것은 나에게이 서버 오류를주고 유지 :컬럼이 존재하지 않습니다 서버 오류 -하지만 SQL 서버 관리가 말합니다

Column 'Previous Login' does not belong to table Table.

Line 47: DateTime userEntry = Convert.ToDateTime(ds1.Tables[0].Rows[0]["Previous Login"]);

그러나 그것은 존재! 내가 같은 쿼리를 사용하여 insql 서버 관리를 잘 작동하므로 반환 데이터가 좋을 것 같아요.

감사합니다.

{ 
    SqlConnection con = new SqlConnection(); 
    con.ConnectionString = "server=(local);database=PhilipsMaterials;Integrated Security=SSPI;"; 
    con.Open(); 
    DataSet ds = new DataSet(); 
    DataTable dt = new DataTable(); 
    // SQL Query that returns only the messages from the last 2 weeks starting with the most recent one. 
    string sql = "Select * From Messages Where DATEDIFF(DAY,Date,GETDATE()) <= 14 ORDER BY Date Desc"; 
    SqlDataAdapter da = new SqlDataAdapter(sql, con); 
    da.Fill(ds); 
    dt = ds.Tables[0]; 
    showMsgs.DataSource = ds; 
    showMsgs.DataBind(); 

    // new message notification 
    string username = Convert.ToString(Session["username"]); 
    username = username.Substring(username.IndexOf("\\") + 1); 

    DataSet ds1 = new DataSet(); 
    DataTable dt1 = new DataTable(); 
    string sqluser = "Select * From [PhilipsMaterials].[dbo].[Employees] Where username='" + username + "'"; 
    SqlDataAdapter da1 = new SqlDataAdapter(sqluser, con); 
    da.Fill(ds1); 
    dt1 = ds1.Tables[0]; 
    DateTime userEntry = Convert.ToDateTime(ds1.Tables[0].Rows[0]["Previous Login"]); 

    DateTime lastMsg = new DateTime(); 
    lastMsg = Convert.ToDateTime(ds.Tables[0].Rows[0]["Date"]); 
    int compared = DateTime.Compare(userEntry, lastMsg); 
    if (compared < 0) { notification.Visible = true; } 
    con.Close(); 
} 
+0

제 생각에 공간이 제거되었거나 다른 문자로 변환되었습니다. 디버거에서'ds1.Tables [0] .Columns'을보고 이름이 무엇인지 확인하십시오. –

+1

제 경험으로 항상 열 이름의 공백을 좋아하지 않았습니다. – Taryn

+3

'WHERE username = ' "+ username +"' "'절대하지 마세요! SqlParamter's for this this. http://en.wikipedia.org/wiki/SQL_injection –

답변

1

당신의 열 이름은 당신이 탈출 어떤 종류의 필요에 공백이 포함되어 있으면 - 당신은 당신의 SQL 문에서 그것을 같은 방식으로 열 이름으로 [Previous Login]를 사용해보십시오. 질문에 대한 코멘트에서 언급 한 바와 같이 - -

그리고

SQL injection and how to avoid it를 보라.

+0

아니요, 괄호로 시도해도 포기했습니다. 중간에 공백이 없도록 컬럼 이름을 바꾼다 - 똑같은 이상한 에러 – Ned

+0

그래, 두 번째 시도. 실패한 라인의 부분적인 표현식을 모두 보았는가? ds1.Tables [0]','ds1.Tables [0] .Rows [0]'및'ds1.Tables [0] .Rows [0] [ "Previous Login"]'예상되는 속성 값을 가진 예상 객체입니까? 조사 창을 사용하여 개체를 검사하십시오. –

+0

데비를 실행할 때마다 감시 창이 비어있을 수 있습니다. gger ... – Ned