使用C#中的SQLite登录表单
本文关键字:登录 表单 SQLite 中的 使用 | 更新日期: 2023-09-27 18:01:10
我在C#中用sqlite生成loginform时遇到问题。这是代码
SQLiteConnection connectionstring;
connectionstring = " Data Source = C:'Crystal Management'Crystal Management'bin'Debug'Konaku.db; Version = 3 ";
public void LoadData()
{
try
{
SQLiteCommand SelectCommand = new SQLiteCommand("SELECT `Username`, `Password` FROM `LoginData` WHERE `Username` = '" + flatTextBox1.Text + "' AND `Password` = '" + flatTextBox2.Text + "'", connectionstring);
SQLiteDataReader myReader;
connectionstring.Open();
myReader = SelectCommand.ExecuteReader();
int count = 0;
while (myReader.Read())
{
count = count + 1;
}
if (count == 1)
{
Base bs = new Base();
bs.Show();
this.Hide();
connectionstring.Close();
}
else if (count == 0)
{
flatAlertBox1.kind = FlatUI.FlatAlertBox._Kind.Error;
flatAlertBox1.Text = "data not right";
connectionstring.Close();
}
else
{
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
connectionstring.Close();
}
}
它显示这行代码有错误
connectionstring = " Data Source = C:''Crystal Management''Crystal Management'bin''Debug''Konaku.db; Version = 3 ";
消息错误为:无法将类型"string"隐式转换为"Finisar.SQLite.SQLiteConnection">
我能用这个做什么?
这是查询SQL的正确方法。对于一次性类,如SQLiteConnection、SQLiteCommand和SQLiteDataReader,始终使用"using"。使用参数化查询可以避免sql注入。
public void LoadData()
{
try
{
using (var conn = new SQLiteConnection(@"Data Source=C:'Crystal Management'Crystal Management'bin'Debug'Konaku.db;Version=3"))
{
conn.Open();
using (var cmd = new SQLiteCommand("SELECT Username,Password FROM LoginData WHERE Username='@username' AND Password = '@password'", conn))
{
cmd.Parameters.AddWithValue("@username", flatTextBox1.Text);
cmd.Parameters.AddWithValue("@password", flatTextBox2.Text);
using (var reader = cmd.ExecuteReader())
{
var count = 0;
while (reader.Read())
{
count = count + 1;
}
if (count == 1)
{
Base bs = new Base();
bs.Show();
Hide();
}
else if (count == 0)
{
flatAlertBox1.kind = FlatUI.FlatAlertBox._Kind.Error;
flatAlertBox1.Text = "data not right";
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
应该是这样的
sql_con = new SQLiteConnection
("Data Source=C:'Crystal Management'Crystal Management'bin'Debug'Konaku.db;Version=3;New=False;Compress=True;");