Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用SQLite在c中登录_C#_Sql_Database_Sqlite_Login - Fatal编程技术网

C# 使用SQLite在c中登录

C# 使用SQLite在c中登录,c#,sql,database,sqlite,login,C#,Sql,Database,Sqlite,Login,我正在尝试使用SQLite数据库创建一个登录表单,我创建了一个名为login的新表,我添加了id pk、username和password字段username和password都是文本作为数据类型,我编写了以下代码: conn.Open(); SQLiteCommand cmd = new SQLiteCommand("select * from login where username = '"+textbox_id.Text+"' and password ='"+textbox_passw

我正在尝试使用SQLite数据库创建一个登录表单,我创建了一个名为login的新表,我添加了id pk、username和password字段username和password都是文本作为数据类型,我编写了以下代码:

conn.Open();
SQLiteCommand cmd = new SQLiteCommand("select * from login where username = '"+textbox_id.Text+"' and password ='"+textbox_password.Text+"'", conn);
cmd.ExecuteNonQuery();
SQLiteDataReader dr = cmd.ExecuteReader();
int count = 0;
while (dr.Read())
        {
            count = count + 1;
        }
if (count == 1)
        {
            MessageBox.Show("K");
        }

我曾尝试使用参数,但结果相同,没有发生任何情况,没有错误,但由于未知原因,计数不会增加。

除了使用参数之外,您正在尝试执行该命令两次。与其使用循环来计算结果,不如使用SQLCount来获得结果的数量

SQLiteCommand cmd = new SQLiteCommand("select Count(*) from login where username = '"+textbox_id.Text+"' and password ='"+textbox_password.Text+"'", conn);
int count = (int)cmd.ExecuteScalar();
但是如果有人在你的任何一个文本框中键入类似“”,那么一定要将其更改为使用参数;删除表登录您的数据库将丢失。更糟糕的是,我可以读取您的所有密码,因为您将它们以明文形式存储,这是另一个大禁忌。

文档:

对于UPDATE、INSERT和DELETE语句,返回值是受命令影响的行数。对于所有其他类型的语句,返回值为-1

因此,这将不起作用:

int count = cmd.ExecuteNonQuery();
您可以使用:


您是否在SQLite management studio中运行该命令以查看查询结果?你有没有在调试器中处理过它?您不应该执行cmd.ExecuteNonQuery,然后再次执行读取器,请选择其中一个,而不是两个。如果您想使用非查询,请使用select count*from login…,这是更好的选择,然后int count=cmd.ExecuteNonQuery就可以了;它返回值-1,尽管当我在manager中运行SQL命令时,它返回count为1I,我的意思是执行标量。
SQLiteCommand cmd = conn.CreateCommand(); 
cmd.CommandText = "select count(*) from ...";
int count = Convert.ToInt32(cmd.ExecuteScalar());