c# - 登录页面 C# .SDF

标签 c# sql-server-ce

我正在尝试创建一个允许用户使用 .sdf 登录的应用程序,互联网上没有太多关于这方面的内容!

真的可以用一些指针来做。

这是我目前拥有的,但我认为它是一团乱麻,因为无论我在每个文本框中键入什么,它都会将我重定向到 Form2(这是意料之中的)。我知道我在某处需要一个 if 语句,但不确定如何实现:

  private void Login_Click(object sender, EventArgs e)
  {
     using (SqlCeConnection yourConnection = new SqlCeConnection("Data Source=C:\\Users\\Username\\Documents\\Databases\\New Folder\\Login.sdf"))
     {
          string query = "SELECT * FROM tbl_employees where Username like '" + textBox1.Text + "' AND Password like '" + textBox2.Text +"'";
          SqlCeDataAdapter dA = new SqlCeDataAdapter(query, yourConnection);
          SqlCeCommandBuilder cBuilder = new SqlCeCommandBuilder(dA);

          this.Hide();
          Form2 secondForm = new Form2();
          secondForm.ShowDialog();
     }
 }      

最佳答案

首先,SQL Server CE 数据库即.sdf 文件只是另一个存储文件。它是非常非常轻便的 SQL Server 版本。

但是,至多,您的代码和逻辑将类似于 SQL Server 的代码和逻辑。只是不同的类(class)。即 SqlCeConnectionSqlCeCommand 等等。

现在您需要验证您的 connectionString 是否正确。

string connectionString ="data source=physical path to .sdf file; 
                         password=pwdThtUSet; persist security info=True";

using (SqlCeConnection yourConnection = new SqlCeConnection(connectionString))
{
       ....your logic
}

现在,在您搜索用户名和密码组合匹配行的查询中,不要使用 like 来执行此操作,因为您需要完全匹配。

就这样吧:

     string query = "SELECT * FROM tbl_employees where Username ='" + textBox1.Text + "' AND Password ='" + textBox2.Text +"'";
      SqlCeDataAdapter dA = new SqlCeDataAdapter(query, yourConnection);
      DataTable dt = new DataTable();
      dA.Fill(dt);

      if(dt.Rows.Count>0)
      {
          this.Hide();
          Form2 secondForm = new Form2();
          secondForm.ShowDialog();
      }

关于c# - 登录页面 C# .SDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15604433/

相关文章:

c# - 我可以为 C# 正则表达式定义自定义模式吗?

asp.net - 在 ASP.NET 网站中使用 SQL Server Compact Edition

c# - 为什么无法在 LinQ 中使用 SqlFunctions 将 int 转换为 string?

.net - SQL Server CE 密码允许使用的字符?

c# - 忽略前导和尾随空格的 View 模型验证

c# - 小数点后 double

sql-server - 将 SDF 数据库转换为 MDF

sql - 如何在 SQL Server Compact Edition 中重命名表?

c# - 将具有内部 IEnumerable 的结构的 IEnumerable 转换为单个字典

c# - HTML敏捷包: Could someone please explain exactly what is the effect of setting the HtmlDocument OptionAutoCloseOnEnd to true?