Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

DB Class for SQLite (See related posts)

// description of your code here

// insert code here..

//examples for handling the class

private void button1_Click(object sender, EventArgs e)
{
string str = Path.GetDirectoryName(Application.ExecutablePath);
handledb mydb = new handledb(str + "\\Trivial.db3");
mydb.AddWord(textBox1.Text);
textBox1.Clear();
}

private void button2_Click(object sender, EventArgs e)
{
string str = Path.GetDirectoryName(Application.ExecutablePath);
dbstuff rt = new dbstuff(str + "\\Trivial.db3");
DataTable dt = rt.ShowAll();
listBox1.DisplayMember = "TWord";
listBox1.DataSource = dt;


}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//textBox2.Text = listBox1.SelectedItems[0].ToString();
//textBox2.Text = listBox1.Items[0].ToString();
textBox2.Text = listBox1.SelectedValue.ToString();

}

private void button3_Click(object sender, EventArgs e)
{
string str = Path.GetDirectoryName(Application.ExecutablePath);
dbstuff rt = new dbstuff(str + "\\Trivial.db3");
rt.RemoveWord(textBox2.Text);
}



//------------ the actual class ----------------


class handledb
{


private SQLiteConnection sqconn;
private string dbpath;

private bool IsNumber(string word)
{
int i;
bool f = int.TryParse(word, out i);
if (f)
return f;
for (i = 0; i < word.Length; i++)
if (char.IsDigit(word[i]))
return true;
return false;
}
private bool IsTrivial(string word)
{
string strSQL = "SELECT * FROM Trivial WHERE Tword='"+word.ToUpper()+"'";
SQLiteCommand sqc = new SQLiteCommand(strSQL, sqconn);
SQLiteDataReader sqr = sqc.ExecuteReader();
bool result = sqr.Read();
sqr.Close();
return result;
}
private void OpenConnection(string dbname)
{

string strconn = "Data Source="+dbpath+";Version=3;";
sqconn = new SQLiteConnection(strconn);
sqconn.Open();
}


public handledb(string dbpath)
{

FileInfo fi = new FileInfo(dbpath);
if (!fi.Exists)
CreatDataBase();
else
OpenConnection();
}


public void CreatDataBase()
{
FileStream fs = File.Create(dbpath);
fs.Close();
OpenConnection();
string strSQL = "Create Table tblitem (" +
"TWord nvarchar(20) PRIMARY KEY NOT NULL" +
")";
SQLiteCommand sqc = new SQLiteCommand(strSQL, sqconn);
sqc.ExecuteNonQuery();
}
public void AddWord(string word)
{
if (IsTrivial(word))
return;
string strSQL = "INSERT INTO Trivial (TWord) VALUES('"+word.ToUpper()+"')";
SQLiteCommand sqc = new SQLiteCommand(strSQL, sqconn);
sqc.ExecuteNonQuery();
}
public void RemoveWord(string word)
{
string strSQL = "DELETE FROM Trivial WHERE Tword='" + word.ToUpper() + "'";
SQLiteCommand sqc = new SQLiteCommand(strSQL, sqconn);
sqc.ExecuteNonQuery();
}
public DataTable ShowAll()
{
string strSQL = "SELECT * FROM Trivial";
SQLiteDataAdapter sqd = new SQLiteDataAdapter(strSQL, sqconn);
DataTable dt = new DataTable();
sqd.Fill(dt);
return dt;
}
public string DBName
{
get { return dbpath; }
}
}
//end class
}

//--------------

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts