[英]How to retrieve return row value from a SQL Server Compact database using C#.NET winform
Table with columns: 带列的表格:
rowID, username, password, administrator,
My code: 我的代码:
SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Visual Studio 2010\Projects\CompactSQL\CompactSQL\DBCompact.sdf;");
con.Open();
SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM DBLogin WHERE username = '" + textBox1.Text + "' AND password = '" + textBox2.Text + "'", con);
cmd.ExecuteNonQuery();
Int32 cnt = (Int32)cmd.ExecuteScalar();
MessageBox.Show(cnt.ToString());
DataTable dt = new DataTable();
SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
da.Fill(dt);
foreach (DataRowView dr in dt.Rows)
{
// how do I get the column result based on the SqlCeCommand I execute?
}
/* ex.: this only work on LinQ to SQL where I use a "foreach" to
gather up results of the column retrieved based on the
query statement executed.
foreach(var r in row)
{
id = r.rowID;
user = r.username;
pass = r.password;
access = r.access;
logID = r.logID
}
*/
I use a SQL Server Compact database for a login form what I want is that when a user login input the username and password on the text field a query is execute to compare if a user exist on the compact db and when found return back the username, access, and logid in a loop with a count of how many row record exist. 我将SQL Server Compact数据库用于登录表单,我想要的是,当用户登录名在文本字段上输入用户名和密码时,执行查询以比较用户是否存在于紧凑型数据库中,并在找到后返回用户名,访问和logid循环存在,并记录存在多少行记录。
Try this 尝试这个
foreach (DataRow dr in dt.Rows)
{
id = dr["rowID"].ToString();
user = dr["username"].ToString();;
pass = dr["password"].ToString();;
access = dr["access"].ToString();;
logID = dr["logID"].ToString();
}
You may need to convert some values to their appropriate type
like Id and logID looks to be int
type. 您可能需要将一些值转换为它们的
appropriate type
例如Id和logID看起来是int
类型。
using linq: you can query the DataTable's Rows collection by adding AsEnumerable() extension. 使用linq:您可以通过添加AsEnumerable()扩展来查询DataTable的Rows集合。 Like so:
像这样:
var row = from rw in dt.AsEnumerable()
select new { id = rw.Field<long>("rowID"),
user = rw.Field<string>("username"),
pass = rw.Field<string>("password"),
access = rw.Field<int>("access"),
logID = rw.Field<int>("logID")};
/* ex.: this only work on LinQ to SQL where I use a "foreach" to
gather up results of the column retrieved based on the
query statement executed.
if(row.Any())
{
foreach(var r in row)
{
id = r.id;
user = r.user;
pass = r.pass;
access = r.access;
logID = r.logID
}
}
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.