如何使用sqlserverce登录 [英] How to login with sqlserverce

查看:49
本文介绍了如何使用sqlserverce登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试使用SqlServerCe作为底层数据库编写登录Windows表单应用程序。问题是我一直在失败。我的语法有问题吗?

自然我添加了对SqlServerCe命名空间的引用,也在参考dll中添加了

请对字符串连接视而不见。这是在测试环境中编写的。

请帮助



我的尝试:



Hi,
I'm trying to write a login Windows form application with SqlServerCe as underlying database. Problem is I have been failing. Is there a fault in my syntax ?
naturally I have added reference to the SqlServerCe namespace and also in the reference dlls
please give a blind eye to the string concatenation. This is written in a testing environment.
Please help

What I have tried:

private void button1_Click(object sender, EventArgs e)
        {
            string lg = "select username, password from creds where username= '" + txtusername + "' and password= '" + txtpassword + "'";

            con.Open();
            SqlCeCommand cmd = new SqlCeCommand(lg, con);
            SqlCeDataReader dr;
            dr = cmd.ExecuteReader();
            int k = 0;
            while (dr.Read())
            {
                k++;
            }

            if (k == 1)
                MessageBox.Show("success!");

            else
                MessageBox.Show("failure");

            con.Close();
        }

推荐答案

using System.Data.SqlClient;

private void button1_Click(object sender, EventArgs e)
    {
        string conString = "Data Source=MyData.sdf;Persist Security Info=False;";
        string lg = "select username, password from creds where username= '" + txtusername + "' and password= '" + txtpassword + "'";

        SqlCeConnection con = new SqlCeConnection();
        con.ConnectionString = conString;
        SqlCeCommand selectCmd = con.CreateCommand();
        selectCmd.CommandText = lg;

        SqlCeDataAdapter adp = new SqlCeDataAdapter(selectCmd);

        DataSet ds = new DataSet();

        // Note: Fill will leave the connection in its original state;
        // In this case, the connection was closed so it will be left closed
        //
        adp.Fill(ds);



        if (ds.Tables[0].Rows.Count > 0)
            MessageBox.Show("success!");

        else
            MessageBox.Show("no data exists");

    }


这篇关于如何使用sqlserverce登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆