Hai *.*,
As i told before here 'm presenting a sample application using SQLITE and C#.i prefer u before moving to an example go through the Introduction to SQLITE.
In a sentence, "SQLite is a in-process library that implements a self-contained, server less, zero-configuration, transactional SQL database engine"
Actually the flow of control is represented here by a diagram so that i can avoid more explanation here.
Flow Of Control
it's a simple application so i don't think much more explanation needed, the above pix is enough.
First a login box will appear with an option create new user
To create a new user, a new window will appear
Here on intilization i'm checking where database exist or not if not create a new one at C:/WINDOWS/
code for that
# region Checking DB exist or not
public void _Cfile()
{
string curFile = @"C:/WINDOWS/WinApp.db";
if (File.Exists(curFile) == true)
{
//if present
}
else
{
SQLiteConnection.CreateFile("C:\\WINDOWS\\WinApp.db");
sqlite = new SQLiteConnection("data source=C:/WINDOWS/WinApp.db");
_CreateTable();
// if not present
}
}
# endregion
look on here i'm creating new database and connection also i'm calling a function to create a table also, here is the code for that.here i'm creating two tables one for storing the login details another for storing the data,'n here its go...
# region Create New Table
private void _CreateTable()
{
try
{
//// User table
string _query="Create Table Mst_User( pk_id INTEGER PRIMARY KEY, str_User TEXT NOT NULL, Str_Password TEXT NOT NULL)";
SQLiteCommand cmd;
sqlite = new SQLiteConnection("data source=C:/WINDOWS/WinApp.db");
sqlite.Open();
cmd = sqlite.CreateCommand();
cmd.CommandText = _query ;
cmd.ExecuteScalar();
/// user data table
string _Query = "CREATE TABLE Mst_UserData( pk_Data INTEGER PRIMARY KEY,str_Data TEXT NOT NULL)";
SQLiteCommand cmde;
sqlite = new SQLiteConnection("data source=C:/WINDOWS/WinApp.db");
sqlite.Open();
cmde = sqlite.CreateCommand();
cmde.CommandText = _Query;
cmde.ExecuteScalar();
}
catch (Exception ex)
{
_Error(ex);
}
}
# endregion
After user entering login data i'm storing it and exit user.
# region Submitting data
public void _SubmitData(string _Uname, string _Pass)
{
try
{
string _Query = "INSERT INTO Mst_User Values (null,'" + _Uname + "','" + _Pass + "')";
SQLiteCommand cmd;
sqlite = new SQLiteConnection("data source=C:/WINDOWS/WinApp.db");
sqlite.Open();
cmd = sqlite.CreateCommand();
cmd.CommandText = _Query;
cmd.ExecuteNonQuery();
txt_Password.Text = "";
txt_UserName.Text = "";
lblErr.Text = "User created Succefully.";
}
catch (Exception ex)
{
_Error(ex);
}
}
# endregion
After the user login, i'm showing data in the database on a grid on form load,
and the code of that is,
# region Bind
private void _bind()
{
SQLiteDataAdapter ad;
DataTable dt = new DataTable();
BindingSource Bsource = new BindingSource();
DataSet ds = new DataSet();
try
{
SQLiteCommand cmd;
sqlite = new SQLiteConnection("data source=C:/WINDOWS/WinApp.db");
sqlite.Open();
cmd = sqlite.CreateCommand();
cmd.CommandText = "SELECT pk_Data as SlNo,str_Data as Data FROM Mst_UserData";
ad = new SQLiteDataAdapter(cmd);
ad.Fill(ds, "Mst_UserData");
Bsource.DataSource = ds.Tables["Mst_UserData"];
dataGridView1.DataSource = Bsource;
}
catch (Exception ex)
{
throw;
}
sqlite.Close();
}
# endregion
# region Submit data
private void btn_Submit_Click(object sender, EventArgs e)
{
try
{
string _data = txt_Data.Text.Trim();
string _query = "INSERT INTO Mst_UserData VALUES( null,'" + _data + "')";
SQLiteCommand cmd;
sqlite.Open();
cmd = sqlite.CreateCommand();
cmd.CommandText = _query;
cmd.ExecuteNonQuery();
txt_Data.Text="";
lblMsg.Text = "Data Added Succefully";
_bind();
}
catch (Exception ex)
{
lblMsg.Text = ex.Message.ToString();
}
}
# endregion
and finally
i prefer u once more before doing an example go through the Introduction to SQLITE. I heard abt SQLITE and i plan to create one 'n i did it that. This is on of my hobby creating some thing i never did.On that crazy i did it so use this code as a reference only because i didn't had much R&D over this.I take 1 hr and 23 min to create this app after hearing abt this.So a sea is infront of me too...
so stay tune...
Have a nice day... 'N happy Coding :)
No comments:
Post a Comment