1

我正在尝试从 mysql 2008 数据库中导入一个字段。我写了这个小代码(我以前用过几次) - 它一直给我这个服务器错误:

“先前登录”列不属于表 Table。

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

但它确实存在!当我在sql服务器管理中使用相同的查询时,它工作正常,所以我猜返回数据很好,知道我哪里出错了吗?

谢谢!

    {
    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();
} 
4

1 回答 1

1

如果您的列名包含空格,您需要某种转义 - 尝试使用[Previous Login]与在 SQL 语句中相同的方式作为列名。

并且 - 正如对问题的评论中提到的那样 - 看看SQL 注入以及如何避免它

于 2013-01-07T19:47:18.320 回答