DerWaldFried
New member
- Joined
- Dec 9, 2019
- Messages
- 2
- Programming Experience
- Beginner
Hello dear, I am currently building a program to learn where I want to display my SQLite data in a table in my windows program.
For this I use a ListBox but also like tips, when a other Solution ist better. My problem is now, after I wrote everything as it should be right. I miss the point where I insert the DataTable data in the table. And I do not know how to do that. Bevor when I needed only small things I have do it like in this example
But how can I get it all now from first name to phone is displayed in the list and so that it is taboo clear?
This is my Code. I hope anything will help me with a Solution that i can learn more. Thanks a lot and i hope too that my english is okay, i am from germany. My English is not the best ^^
For this I use a ListBox but also like tips, when a other Solution ist better. My problem is now, after I wrote everything as it should be right. I miss the point where I insert the DataTable data in the table. And I do not know how to do that. Bevor when I needed only small things I have do it like in this example
C#:
klist.ValueMember = "DataID"
But how can I get it all now from first name to phone is displayed in the list and so that it is taboo clear?
C#:
namespace BaldwinVS
{
public partial class BaldwinVS : Form
{
public BaldwinVS()
{
InitializeComponent();
}
private void BaldwinVS_Load(object sender, EventArgs e)
{
if (!Directory.Exists("Daten"))
{
System.IO.Directory.CreateDirectory("Daten");
SQLiteConnection.CreateFile("Daten/vsdaten.sqlite");
SQLiteConnection dbConnection = new SQLiteConnection("Data Source = Daten/vsdaten.sqlite; Version = 3;");
dbConnection.Open();
string sql = "CREATE TABLE personen(vorname TEXT, nachname TEXT, ausweissnummer TEXT, telefon NUMERIC)";
SQLiteCommand Command = new SQLiteCommand(sql, dbConnection);
Command.ExecuteNonQuery();
}
else
{
if (!File.Exists("Daten/vsdaten.sqlite"))
{
SQLiteConnection.CreateFile("Daten/vsdaten.sqlite");
SQLiteConnection dbConnection = new SQLiteConnection("Data Source = Daten/vsdaten.sqlite; Version = 3;");
dbConnection.Open();
string sql = "CREATE TABLE personen(vorname TEXT, nachname TEXT, ausweissnummer TEXT, telefon NUMERIC)";
SQLiteCommand Command = new SQLiteCommand(sql, dbConnection);
Command.ExecuteNonQuery();
}
else
{
SQLiteConnection dbConnection = new SQLiteConnection("Data Source = Daten/vsdaten.sqlite; Version = 3;");
dbConnection.Open();
list_load();
}
}
}
private ArrayList list_load()
{
string sSql = "SELECT * FROM personen";
SQLiteConnection dbConnection = new SQLiteConnection("Data Source = Daten/vsdaten.sqlite; Version = 3;");
SQLiteCommand Command = new SQLiteCommand(sSql, dbConnection);
ArrayList list = new ArrayList();
try
{
DataTable table = GetDataTable(sSql);
// Return all table names in the ArrayList
foreach (DataRow row in table.Rows)
{
list.Add(row.ItemArray[0].ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return list;
}
private DataTable GetDataTable(string sSql)
{
SQLiteConnection dbConnection = new SQLiteConnection("Data Source = Daten/vsdaten.sqlite; Version = 3;");
try
{
DataTable dt = new DataTable();
using (var c = new SQLiteConnection(dbConnection))
{
c.Open();
using (SQLiteCommand cmd = new SQLiteCommand(sSql, c))
{
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
dt.Load(rdr);
return dt;
SQLiteDataAdapter sql_adapt = new SQLiteDataAdapter(sSql, dbConnection);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
}
}