I have installed SQLite and am using the wrapper from: http://sqlite.phxsoftware.com/
I have created my database and table in the server explorer in VS2010 but when I create the connection, I don't understand what to do from there and how to get it to work.
Can someone provide code examples on how to connect to a db, get a table, insert data into the table and select data from a table and output it.
I would really appreciate it.
Thanks
Server Explorer is overkill, SQLite is meant to be simple and light. Just use plain code as follows.
SQLiteConnection connection = new SQLiteConnection("Data source=PATH_TO_YOUR_DB;Version=3");
connection.Open();
SQLiteCommand command = connection.CreateCommand();
command.CommandText = "insert into something values (1,2,3)";
command.ExecuteNonQuery();
connection.Close();
Hope it helps.
OK here is what I did (assuming that you have that s**t installed):
1.-right click on server explorer
2.-then click on add conection
3.-on data source click Change then select sqlite
4.-fill out da details
5.-you are done... you can now add datasets....
Related
I just added a new "local database" in my project using visual studio. Now I added the "New Data Source".
What I want to know is how you can do sql queries to the DB. I know how this is done in PHP but cant find good information about how to do this in C#. All I find is tutorials on how to drag detail or gridviews into the form.
I have one database called 'words' with three tables. I want to be able to do update queries to a row in each of these tables. something like this:
UPDATE easy SET words='blahblahblah' WHERE id=1;
How do you do this?
I am going to use SQL Server as example.
First you need to find out the connection string which should be similar to
_connectionString = "Data Source=(localdb)\\v11.0; Initial Catalog=words;Integrated Security=true;"
and then you do
using (SqlConnection connection = new SqlConnection(_connectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("UPDATE easy SET words='blahblahblah' WHERE id=1;", connection))
{
cmd.ExecuteNonQuery();
}
}
We want to add a table programmatically to our local stored Microsoft SQL Server Compact 3.5 Database. The code below creates the table.
using (SqlCeConnection con =
new SqlCeConnection("Data Source=|DataDirectory|\\Database.sdf"))
{
con.Open();
using (SqlCeCommand com =
new SqlCeCommand("create table test (id int not null)", con))
{
Console.WriteLine("Response: " + com.ExecuteNonQuery());
}
con.Close();
}
The code is working fine, but the table isn't listed up in the Server Explorer of the specified database table. We can insert values into the table and read data out of the table.
Do you know any solutions for this problem?
Afterwards we want to add a dynamic datamodel, which we want to use as a provider of our tables.
Thank you in advance.
The use of |DataDirectory| means that you have 2 copies of the file in your project folders.
Your application is using the one in Root\bin\debug.
Your tools are looking in \Root.
after connecting to database in C#
string MyConString2 = "SERVER=localhost;" + "user id=mytest;" + "DATABASE=clusters;" + "PASSWORD=mypass;";
I have an algorithm which I need to after each run of algorithm that will fill the database, drop the database "clusters" of mysql manually and again connect to the empty database and run it again,gaining new data in tables
I want to make it automatically how can I drop or empty my database if exists in C# and then run my algorithm?
Here is example code that works and I think this is what you are talking about, if not, feel free to correct me.
using (var connection = new MySqlConnection("server=localhost;user=root;password="))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "drop schema if exists clusters";
command.ExecuteNonQuery();
command = connection.CreateCommand();
command.CommandText = "create schema clusters";
command.ExecuteNonQuery();
}
Prepare sql query for clearing your DB and test it in f.e. MySQL workbench. Following this, just execute it as you would execute regular query against DB in C#. One way is to clear all the tables in your database by using TRUNCATE statement and the second way is to DROP DATABASE and recreate it.
Right, I have been tasked with developing a new application in MVC3 that unfortunately has to integrate very slightly with a classic asp web site. This won't be forever as the old site will get an update at some point, but not yet. In the mean time however the new MVC3 application will need a little bit of access to the database for the old site, which is a old MS Access .mdb whereas the new app will be using sql server 2008.
I would greatly appreciate it if someone could give me some examples of how to connect to the access db, aswell as how to execute sql queries (i am fine writing the sql, just got no idea how to execute against the database from my mvc3 app).
thanks in advance
EDIT: I've not got much experience with the old site, but it appears to use the JET adaptor if that helps! ;-)
Your question requires an answer too extensive to be given in detail
I will give you a check list of things and class to research
Define the connection string used to reach your database [see
here]
Create and open the OleDbConnection
Define your OleDbCommand and the command text to be executed
Create and use an OleDbDataReader to read your data line by line
Create and use an OleDbDataAdapter to read your data and load a
DataSet or DataTable
Now don't forget to close your connection and use parametrized query
string connectionString = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;Jet OLEDB:Database Password=MyDbPassword;
public void InsertRow(string connectionString, string insertSQL)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
// The insertSQL string contains a SQL statement that
// inserts a new row in the source table.
OleDbCommand command = new OleDbCommand(insertSQL);
// Set the Connection to the new OleDbConnection.
command.Connection = connection;
// Open the connection and execute the insert command.
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// The connection is automatically closed when the
// code exits the using block.
}
}
I have PostgresQL database, and I develop interface application using C# and Npgsql to connect to the database, how can I assign the Npgsql connection to TableAdapter?
Thanks,
I'm in the exact same boat with PostgreSQL.
I don't believe that's possible. Npgsql has its own data adapter (see http://npgsql.projects.postgresql.org/docs/manual/UserManual.html and search for text "adapter"). The downside to this is that you can't use the Visual Studio Designer.
So instead, I'm using .NET's ODBC DataSource. For this to work, you will need to install the postgresql odbc driver, which is available here: http://www.postgresql.org/ftp/odbc/versions/msi/. After installing, you can go to Control Panel -> Administrative Tools -> Data Soruces (ODBC) to add a DSN (Data Source Name). Finally, in Visual Studio go to the Server Explorer, right click "Data Connections" and select "Add Connection...", and change the data source of Microsoft ODBC Data Source. Here you can select the DSN you provided earlier, and viola! You're in business.
(Note that for some crazy reason bools come in as strings. You can change this in the ODBC Data Source Administrator by clicking "Configure" on your PostgreSQL datasource, going to Datasource options, and unchecking "Bools as Char".)
Sorry its a bit late however here is how you would do it, use the data adapter.
create a datagrid
create a datatable
fill the datatable with the dataAdapter, // function is provided - dataTableName.fill()
set the datagrid to display default view of the dataTable
try
{
using (var Connection = new NpgsqlConnection(PG_Connection_String))
NpgsqlDataAdapter da = new NpgsqlDataAdapter("myQuery", connectionString))
{
Connection.Open();
myTable = new System.Data.DataTable();
da.Fill(myTable);
postgresql_dataGrid.DataSource = myTable.DefaultView;
Connection.Close();
}
}
catch (Exception Ex)
{
MessageBox.Show("Your Error", "Connection Error");
}
}
Bob's your uncle, I'm using it successfully. I would suggest that if you wish to prepare the grid for custom headers etc, do it BEFORE setting the default view of your datagrid