i have problem with sqlite !
in my c# app i using a little database based on sqlite, i can select and see database data but the problem is that and when i try to insert a record into the table i see it in the program and it's worked well but and added to my DB but when i close the program and open the DB nothing saved :( and when i'm using my app this seems it's added to database but in realtime i'm using firefox addons to see database or when close and open app nothing saved.
SQLiteConnection ObjConnection = new SQLiteConnection("Data Source=Data/data.db3;");
private void button1_Click(object sender, EventArgs e)
{
SQLiteCommand ObjCommand = new SQLiteCommand("SELECT * FROM PERSON", ObjConnection);
ObjCommand.CommandType = CommandType.Text;
SQLiteDataAdapter ObjDataAdapter = new SQLiteDataAdapter(ObjCommand);
DataSet dataSet = new DataSet();
ObjDataAdapter.Fill(dataSet, "Person");
dataGridView1.DataSource = dataSet.Tables["Person"];
}
private void button2_Click(object sender, EventArgs e)
{
string name = textBox1.Text;
string last = textBox2.Text;
string q1 = "INSERT INTO Person(Firstname,Lastname) VALUES(?,?)";
SQLiteCommand cmd1 = new SQLiteCommand(q1, ObjConnection);
cmd1.Parameters.AddWithValue("#SongName",name);
cmd1.Parameters.AddWithValue("#ArtistName",last);
ObjConnection.Open();
cmd1.ExecuteNonQuery();
ObjConnection.Close();
SQLiteCommand ObjCommand = new SQLiteCommand("SELECT * FROM PERSON", ObjConnection);
ObjCommand.CommandType = CommandType.Text;
SQLiteDataAdapter ObjDataAdapter = new SQLiteDataAdapter(ObjCommand);
DataSet dataSet = new DataSet();
ObjDataAdapter.Fill(dataSet, "Person");
dataGridView1.DataSource = dataSet.Tables["Person"];
}
private void button3_Click(object sender, EventArgs e)
{
ObjConnection.Open();
ObjConnection.ChangePassword("123");
ObjConnection.Close();
}
It seems a problem with the connection String, SQLite would be using in your case a temporary database. Temporary Databases are automatically deleted when the connection that created them closes.
Data Source=Data/data.db3;
Just to discard my solution, could you try with an exiting absolute path (c:\Data):
Data Source=c:\\Data\\data.db3;
Related
I'm building a C# Windows Forms database application. Data should be written in a text field and saved in a table using a button. My problem is that the data is not saved in the table.
I have built a list that should show the records of the table. When you click on the insert button, the text is displayed in the list, but it is not saved in the table.
My code:
private void insert_Click(object sender, EventArgs e)
{
string con_string = Properties.Settings.Default.DB1_ConnectionString;
SqlConnection con = new SqlConnection(con_string);
con.Open();
SqlCommand cmd = new SqlCommand("insert into tab_Musik values (#Titel,#Inerpret,#Genre)", con);
cmd.Parameters.AddWithValue("#Titel", int.Parse(txt_Titel.Text));
cmd.Parameters.AddWithValue("#Inerpret", txt_Interpret.Text);
cmd.Parameters.AddWithValue("#Genre", double.Parse(kmb_Genre.Text));
cmd.ExecuteNonQuery();
con.Close();
}
private void show[enter image description here][1]_Click(object sender, EventArgs e)
{
string con_string = Properties.Settings.Default.DB1_ConnectionString;
SqlConnection con = new SqlConnection(con_string);
con.Open();
SqlCommand cmd = new SqlCommand("select * from tab_Musik", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource=dt;
}
For the problem you described about clicking the insert button, the data cannot be saved to the table, you can try the following code:
private void btnAdd_Click(object sender, EventArgs e)
{
string con_string = "database connection string";
SqlConnection sqlConnection = new SqlConnection(con_string);
string myinsert = "insert into tab_Musik values (#Titel,#Inerpret)";
SqlCommand mycom = new SqlCommand(myinsert, sqlConnection);
mycom.Parameters.AddWithValue("#Titel", textBox1.Text);
mycom.Parameters.AddWithValue("#Inerpret", textBox2.Text);
sqlConnection.Open();
mycom.ExecuteNonQuery();
mycom.Clone();
mycom.Dispose();}
Show results:
So i've been looking around the google for the anwser to this found so many diffrenet anwsers but i do not quite understand them and dont really see the way to implement them even thoug i have tryed alot so my issue is basicly that my delete button only deletes from the datagridview and not the database itself i do have the gridview bound to my knowledge but this is the very first form app i make so im a bit puzzeld to what i am doing
private void buttonDel_Click(object sender, EventArgs e)
{///////////////////////////////////////////////////////issue is here
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
dataGridView1.Rows.RemoveAt(item.Index);
}
//string del = "DELETE FROM Data WHERE RowID = #RowID";
}
using (SqlConnection Connection = new SqlConnection(Connectionstring))
{
string query = "insert into data(Navn, NummerPlade, KMKørt, dato)";
query += " values (#Navn, #NummerPlade, #KMKørt, #dato)";
Connection.Open();
SqlCommand cmd = new SqlCommand(query, Connection);
cmd.Parameters.AddWithValue("#Navn", textBox1.Text);
cmd.Parameters.AddWithValue("#NummerPlade", textBox8.Text);
cmd.Parameters.AddWithValue("#KMKørt", textBox6.Text);
cmd.Parameters.AddWithValue("#dato", textBox7.Text);
cmd.ExecuteNonQuery();
Connection.Close();
button4_Click(sender, e);
}
private void button4_Click(object sender, EventArgs e)
{
/// Connect / Update
using (SqlConnection Connection = new SqlConnection(Connectionstring))
{
Connection.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM Data", Connection);
DataTable data = new DataTable();
sqlDa.Fill(data);
dataGridView1.DataSource = data;
}
buttonConn.Hide();
}
I am trying to fill a datagridview with content from a .mdf SQL Server database file (C# Windows Forms app)...
private void Companies_Load(object sender, EventArgs e)
{
load_table();
}
void load_table()
{
String DATA = Application.StartupPath + #"\data.mdf";
string constring = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + DATA + ";Integrated Security=True";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand("select * from Companies ;", conDataBase);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);
}
catch (Exception uu)
{
MessageBox.Show(uu.Message);
}
}
I get nothing. DataGridView is empty. No errors...
Table name: Companies with 4 rows and 1 column...
I tried SQL statements like
select * from dbo.Companies ;
... still nothing
I changed data.mdf connection to full path c:/etc/etc ...
No luck.
Any simple solution is welcome :)
.mdf is a SQL Server data file, therefore you need to use the SQL Server client library, e.g. SqlConnection, SqlCommand and SqlDataAdapter.
What you're using now (MySqlConnection, MySqlCommand, MySqlDataAdapter) is for MySQL and won't work on a (Microsoft) SQL Server data file.
I am at the end of the rope...
I am taking a value from a dateTimePicker and I am executing a SQL query based on the value from the dateTimePicker. The result of the query should be displayed on a Datagridview after executing the query.
Here is the code I have made so far:
namespace App1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DateTime varDate;
varDate = dateTimePicker1.Value;
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=SUBASH-LAPTOP\COBRA;Initial Catalog=Test;Integrated Security=True");
SqlCommand Command = con.CreateCommand();
Command.CommandText = "Select * From orders where date_purchased <= #varDate ";
con.Open();
SqlDataReader reader = Command.ExecuteReader();
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add();
DataGridView d1 = new DataGridView();
this.Controls.Add(d1);
while (reader.Read())
{
d1.DataSource = ds.Tables[0];
}
}
}
I pretty inexperienced with c# so any answers would be most appreciated.
Please help.
Thanks,
Subash
use this code:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=SUBASH-LAPTOP\COBRA;Initial Catalog=Test;Integrated Security=True");
SqlCommand Command = con.CreateCommand();
SqlDataAdapter dp = new SqlDataAdapter("Select * From orders where date_purchased <= #varDate", con);
dp.SelectCommand.Parameters.AddWithValue("#varDate", dateTimePicker1.Value);
DataSet ds = new DataSet();
dp.Fill(ds);
DataGridView d1 = new DataGridView();
d1.DataSource = ds;
d1.DataMember = ds.Tables[0].TableName;
this.Controls.Add(d1);
}
Check out the Link below:
How to: Bind Data to the Windows Forms DataGridView Control
the DataGridView object reads in data from a "binding source" you have to link that to a TableAdapter that contains your SQL SELECT statement.
You're about 1/2 the way there. Getting the connection string working correctly is half the battle. The other half is finding out where your data is in relation to the DatagridView.
good Luck!
You have to add the parameter to the SqlCommand for it to be used:
Command.Parameters.AddWithValue("#varDate", DateTimePicker1.Value);
as i saw your code, you are missing assignment of parmeter value,
Command.CommandText = "Select * From orders where date_purchased <= #varDate ";
after this line you have to provide parmeter value to command object.
Command.Parameters.AddWithValue("#varDate", DateTimePicker1.Value);
I'm new to MySQL and now i hava a project which must work with MYSQL. It's a win application and I use C# on Visual Studio 2010. I tried to write a simple stored procedure like this:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `GetBank`()
BEGIN
SELECT * FROM BANKNAME;
END
And my C# code is here:
private void btnShow_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection("SERVER=localhost;DATABASE=dowacodb;UID=root;PASSWORD=123456");
con.Open();
DataTable dt = new DataTable();
MySqlCommand cm = new MySqlCommand("GetBank", con);
cm.CommandType = CommandType.StoredProcedure;
MySqlDataAdapter da = new MySqlDataAdapter(cm);
da.Fill(dt);
con.Close();
dataGridView1.DataSource = dt;
}
My test is to show the data in datagridview when clicking the button. It shows fine on the first click but when I click again, the data in datagridview is gone. The next click will show the data again and repeatedly.
But this code will be perfect when not working with stored procedured
private void btnShow_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection("SERVER=localhost;DATABASE=dowacodb;UID=root;PASSWORD=123456");
con.Open();
DataTable dt = new DataTable();
MySqlCommand cm = new MySqlCommand("Select * from bankname", con);
cm.CommandType = CommandType.Text;
MySqlDataAdapter da = new MySqlDataAdapter(cm);
da.Fill(dt);
con.Close();
dataGridView1.DataSource = dt;
}
So what's wrong with MySQL? Thanks in advance
The syntax to call your procedure in MySQL is
CALL GetBank()
so
MySqlCommand cm = new MySqlCommand("CALL GetBank()", con);
should do it. Which also removes the need to specify that your command cm is a stored procedure.
This problem is solved! Instead of using ordinary way to interact with MySQL: open a connection, create a command and use a data adapter to fill datatable, I use a MySQLHelper class and it works fine for me:
DataTable dt = MySqlHelper.ExecuteDataset("SERVER=localhost;DATABASE=dowacodb;UID=root;PASSWORD=123456", "call getbank()").Tables[0];
dataGridView1.DataSource = dt;
But now I wonder why this way works while the older way doesn't ?