如何使用 C#.NET winform 从 SQL Server Compact 数据库中检索 return 行值
How to retrieve return row value from a SQL Server Compact database using C#.NET winform
Table 列:
rowID, username, password, administrator,
我的代码:
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
}
*/
我使用 SQL Server Compact 数据库作为登录表单 我想要的是当用户登录时在文本字段中输入用户名和密码时执行查询以比较用户是否存在于压缩数据库,当找到时 return 在一个循环中返回用户名、访问权限和 logid,并计算存在多少行记录。
试试这个
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();
}
您可能需要将某些值转换为它们的 appropriate type
,例如 Id 和 logID 看起来是 int
类型。
使用 linq:您可以通过添加 AsEnumerable() 扩展来查询 DataTable 的 Rows 集合。像这样:
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
}
}
*/
Table 列:
rowID, username, password, administrator,
我的代码:
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
}
*/
我使用 SQL Server Compact 数据库作为登录表单 我想要的是当用户登录时在文本字段中输入用户名和密码时执行查询以比较用户是否存在于压缩数据库,当找到时 return 在一个循环中返回用户名、访问权限和 logid,并计算存在多少行记录。
试试这个
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();
}
您可能需要将某些值转换为它们的 appropriate type
,例如 Id 和 logID 看起来是 int
类型。
使用 linq:您可以通过添加 AsEnumerable() 扩展来查询 DataTable 的 Rows 集合。像这样:
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
}
}
*/