1

mysql 2008 データベースからフィールドをインポートしようとしています。私はこの小さなコードを書きました(数回前に使用しました)-そして、このサーバーエラーが発生し続けます:

列「前回のログイン」はテーブル Table に属していません。

47 行目: DateTime userEntry = Convert.ToDateTime(ds1.Tables[0].Rows[0]["前回のログイン"]);

しかし、それは存在します!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 に答える