Database 如何将在VisualStudio中创建的SQLite数据库与表单的代码连接起来?
输出表明,Database 如何将在VisualStudio中创建的SQLite数据库与表单的代码连接起来?,database,forms,sqlite,connection,Database,Forms,Sqlite,Connection,输出表明,dkw未找到任何连接 以下代码段分别用于类数据库和表单的代码: public class DataBase { SQLiteConnection cnx; // Creates an empty database file public void createNewDatabase() { SQLiteConnection.CreateFile("MyDatabase.sqlite"); } /
dkw未找到任何连接
以下代码段分别用于类数据库和表单的代码:
public class DataBase
{
SQLiteConnection cnx;
// Creates an empty database file
public void createNewDatabase()
{
SQLiteConnection.CreateFile("MyDatabase.sqlite");
}
// Creates a connection with our database file.
public SQLiteConnection connectToDatabase()
{
cnx = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
cnx.Open();
return cnx;
}
public void Close()
{
cnx.Close();
}
public void createTable()
{
string sql = "create table Users (username varchar(50), password varchar(15)";
SQLiteCommand command = new SQLiteCommand(sql,cnx);
command.ExecuteNonQuery();
}
}
public partial class UserLogin : Form
{
DataBase db = new DataBase();
SQLiteConnection cnx;
private void Form1_Load(object sender, EventArgs e)
{
db.createNewDatabase();
cnx = new SQLiteConnection(db.connectToDatabase());
db.createTable();
}
public UserLogin()
{
InitializeComponent();
textBox2.PasswordChar = '*';
textBox2.MaxLength = 15;
}
private void exit_button_Click(object sender, EventArgs e)
{
this.Hide();
Application.Exit();
}
private void register_button_Click(object sender, EventArgs e)
{
try
{
//Inserta un nuevo usuario en la base de datos
Console.WriteLine("Base de datos abierta");
string username = textBox1.Text;
string password = textBox2.Text;
string query = "Insert into Users (username , password) values ('" + username + "','" + password + "')";
SQLiteCommand command = new SQLiteCommand(query, cnx);
int resultado = command.ExecuteNonQuery();
if (resultado == 1)
{
MessageBox.Show("Insercion realizada");
}
else
{
MessageBox.Show("Error inserción");
}
//Cierra conexión con la base de datos
cnx.Close();
}
catch (Exception o)
{
MessageBox.Show("error" + o.Message);
}
textBox1.Text = "";
textBox2.Text = "";
}
private void login_button_Click(object sender, EventArgs e)
{
string username = textBox1.Text;
string password = textBox2.Text;
bool authentication = false;
try
{
//En este objeto (resultado tabla) va a ir a parar toda la informacion extraida
DataTable res = new DataTable();
if (username != "" && password != "")
{
SQLiteDataAdapter adp = new SQLiteDataAdapter ("SELECT Username,Password FROM Users WHERE (Username = '" + username + "' and Password = '" + password + "')", cnx);
// Si encuentra el usuario en la base de datos rows valdrá 1
int rows = adp.Fill(res);
if (rows == 1)
{
authentication = true;
}
}
cnx.Close();
}
catch (Exception o)
{
MessageBox.Show("error" + o.Message);
}
// Si el usuario existe y la contraseña es correcta crea un nuevo formulario principal y lo muestra en pantalla
if (authentication)
{
Principal form = new Principal();
form.Show();
this.Hide();
}
else
{
MessageBox.Show("The user doesn't exist or de data isn't well introduced");
}
}
}