简体   繁体   English

如何使用C#.NET Winform从SQL Server Compact数据库检索返回行值

[英]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.

相关问题 如何从数据库(SQL Server)动态创建MenuStrip(在Winform C#.Net中)? - How to Create MenuStrip dynamic (in winform C#.Net) From Database (SQL Server)? 从C#.NET Winform应用程序重新定位SQL Server中的查询 - Repitition of a query in sql server from C#.NET Winform application 如何使用C#.net从数据库中检索日期值并在HTML5输入类型“日期”中进行设置? - How to retrieve Date value from Database using C#.net and set in HTML5 input Type 'date'? 如何使用 c#.net 从 combobox 中的 sql 服务器获取所有数据库的列表 - How to get list of all database from sql server in a combobox using c#.net 我们如何在不使用 DAO 和 JRO 的情况下在 VB.NET 或 C#.NET 中压缩访问数据库 - How can we compact a access database in VB.NET or C#.NET without using DAO and JRO 努力将记录从我的C#WinForm应用程序插入“ SQL Server Compact 4.0数据库”。 (我正在使用VS 2010) - Struggling to insert record into “SQL Server Compact 4.0 Database” from my C# WinForm Application. (I'm using VS 2010)) 使用SQL Server Express为ac#.net winform应用程序创建多个用户 - creating multiple users for a c#.net winform application using sql server express 将TLS 1.2与C#.Net Winform App和SQL Server 2008一起使用 - Using TLS 1.2 with C#.Net Winform App and SQL Server 2008 如何使用 C# 从 SQL Server 数据库中逐行检索数据到 datagridview - How to retrieve data into a datagridview row by row from a SQL Server database using C# 使用C#.Net从SQL Server数据库读取的最快方法 - Fastest way to read from sql server database using C#.Net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM