我正在编写一个WPF C#应用程序,我想在变量中保存已获取的行。
SqlDataAdapter connAdp = new SqlDataAdapter("SELECT * FROM users WHERE (username = '"+uname+"') AND (password = '"+pass+"');",conn);
SqlDataReader fetchInfo = null;
fetchInfo = conn.ExecuteReader();
fetchInfo.Read();
但是当我尝试构建我的应用程序时,它会给出错误
未定义ExecuteReader
我也试过这段代码:
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DataRow row = dt.Rows["username"];
但它也给出了错误。我是C#和WPF的新手,所以有人能告诉我如何从SQL Server获取数据并存储在变量或数组中吗?
答案 0 :(得分:0)
我自己解决了
SqlCommand cmd = new SqlCommand("SELECT * FROM users WHERE (username = '" + uname + "') AND (password = '" + pass + "');", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
这是男人们,我希望它有助于新手。您可以使用dr.Read()
答案 1 :(得分:0)
始终是SqlCommand.ExecuteReader而不是Sqlconnection.ExecuteReader。
SqlCommand cmd= new SqlCommand("SELECT * FROM users WHERE (username = '"+uname+"') AND (password = '"+pass+"');",conn);
conn.open();
SqlDatareader dr = cmd.ExecuteReader();
While(dr.Read())
{
//Fecth your values from reader and store it in your variable
}
conn.close();