【发布时间】:2012-12-21 14:11:15
【问题描述】:
我正在尝试从 mysql 2008 数据库中导入一个字段。 我写了这个小代码(我以前用过几次) - 它一直给我这个服务器错误:
列'Previous Login'不属于表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();
}
【问题讨论】:
-
我的猜测是空格被删除或转换为不同的字符。在调试器中查看
ds1.Tables[0].Columns,看看名字是什么。 -
我的经验一直是它不喜欢列名中的空格。
-
WHERE username='" + username + "'"不要这样做!永远不能!为此使用SqlParamters。 en.wikipedia.org/wiki/SQL_injection -
嗨丹尼尔,谢谢你的建议,我会看看但这个项目实际上不会在网络上使用,所以安全性是一个非常低的优先级(需要先实际工作) ://
标签: c# asp.net mysql sql datatable