[英]Login form with SQLite in C#
我在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_con = new SQLiteConnection
("Data Source=C:\Crystal Management\Crystal Management\bin\Debug\Konaku.db;Version=3;New=False;Compress=True;");
这是查询SQL的正确方法。 始终对一次性类(如SQLiteConnection,SQLiteCommand和SQLiteDataReader)使用“使用”。 使用参数化查询来避免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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.