Fill data table using access - C# - c#

I built a connection string to access data from a database to fill a datatable.
connectionname = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=d:\\projects\\checking.mdb; OLE DB Services=-1"
DataTable results = new DataTable();
using (OleDbConnection thisConnection = new OleDbConnection(connectionname))
{
OleDbCommand cmd = new OleDbCommand("SELECT * from PatTestTable", thisConnection);
thisConnection.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(results);
}
There is no error and in the debug mode, I can see the connection string being built correctly but the results datatable is empty. Did I miss any step?

Related

How to select non-English characters from US7ASCII type DB use C# OLEDB

The character set of the target Oracle DB is US7ASCII, but Korean characters are stored.
When I select the data In my C# program using OLEDB and look at the datagridview, Korean characters are shown as ???.
Environment.SetEnvironmentVariable("NLS_LANG", "AMERICAN_AMERICA.KO16KSC5601");
string connectionString = "Provider=OraOLEDB.Oracle;Data Source=DB;User ID=ID;Password=PW;";
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
OleDbCommand command = new OleDbCommand();
command.CommandText = "SELECT nm FROM table";
command.Connection = connection;
DataTable dataTable = new DataTable();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command);
dataAdapter.Fill(dataTable);
dataGridView1.DataSource = dataTable;
connection.Close();
The code above is e.g. of my program. I need help to solve this.

C# DataTable update Access Database

How can I save a DataTable to a file. accdb (Access) existing one? I've used the following code and it does not work:
using (OleDbConnection oledbConnection = new OleDbConnection(connection))
{
oledbConnection.Open();
string query = "SELECT * FROM Student";
using (OleDbCommand oledbCommand = new OleDbCommand(query, oledbConnection))
{
using (OleDbDataAdapter oledbDataAdapter = new OleDbDataAdapter(oledbCommand))
{
using (OleDbCommandBuilder oledbCommandBuilder = new OleDbCommandBuilder(oledbDataAdapter))
{
oledbDataAdapter.DeleteCommand = oledbCommandBuilder.GetDeleteCommand(true);
oledbDataAdapter.InsertCommand = oledbCommandBuilder.GetInsertCommand(true);
oledbDataAdapter.UpdateCommand = oledbCommandBuilder.GetUpdateCommand(true);
oledbDataAdapter.Update(dataTable);
}
}
}
oledbConnection.Close();
}
The variable dataTable is initialized with the original contents of the file, then it was modified by adding a row and now I have to update the table in the database.
I tried using the following code, but that does not work :(
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM Student", connection);
OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(da);
da.InsertCommand = cmdBuilder.GetInsertCommand(true);
// create and insert row in the DataTable
da.Update(dataTable);
Assuming that you have made some changes in the datatable, then you could pass the generated update/insert/delete command to the adapter in this way
oledbDataAdapter.DeleteCommand = oledbCommandBuilder.GetDeleteCommand();
oledbDataAdapter.InsertCommand = oledbCommandBuilder.GetInsertCommand();
oledbDataAdapter.UpdateCommand = oledbCommandBuilder.GetUpdateCommand();
oledbDataAdapter.Update(datatable);
Now the adapter knows how to update your table

How to use parameters on the following method

I just ran Visual Studio's Code analysis, they told me to use a parameterized query for the following line of code:
using(var DA = new SQLiteDataAdapter(SQL, connectionstring))
The complete method:
public static DataTable SelectAll(string SQL)
{
//create a string(connectionstring), filled with the connectionstring
string connectionstring = "Data Source=" + LogicDatabase.Databasename + ";Version=3;Password=" + Settings.SQLiteDatabasePassword + ";";
//create a new DataSet
DataSet DS = new DataSet();
//Give name to a SQLiteDataAdapter
//Create a new SQLiteDataAdapter and fill it with the sql query, and path of the Database.
using(var DA = new SQLiteDataAdapter(SQL, connectionstring))
{
//Clear the dataset, so we are sure it is empty, before storing items in it.
DS.Clear();
//fill the dataset from the SQLiteDataAdapter.
DA.Fill(DS);
//Fill the DataTable with the first table of the DataSet
DataTable DT = DS.Tables[0];
//return the DataTable
return DT;
}
}
How can I implement parameters in this code?
Use the SQLiteCommand and fill it with your SQL statement. It has a property Parameters which you can fill with instances of SQLiteParameter. After that, you can use this command to create your SQLiteDataAdapter.

Create/Display DataGrid from a table (database) C#

I'm trying show a DataGrid in C# (for app WindowsMobile). I have a database ("pruebaDB.sdf") in DataConnections and one table ("tablaMercancia").
Also in DataSource I have "pruebaDBDataSet" and "tablaMercancia".
How I can show data table in a DataGrid?
I use a SmartDevice project (I can't to use DataGridView, only I use DataGrid).
I can show a new table (created for code) in a DataGrid, but I don't know to show an existing table in my database.
string conSTR = "Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) + "\\pruebaDB.sdf;Persist Security Info=False";
SqlCeConnection connection = new SqlCeConnection(conSTR);
string sql = "SELECT * FROM tablaMercancia";
connection.Open();
SqlCeCommand cmd = new SqlCeCommand(sql, connection);
SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
//...............
//...Any idea?
//...............
connection.Close();
Any ideas please?
Thanks!!!
Please, Change Datagridview name as per below :
string conSTR = "Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) + "\\pruebaDB.sdf;Persist Security Info=False";
SqlCeConnection connection = new SqlCeConnection(conSTR);
string sql = "SELECT * FROM tablaMercancia";
connection.Open();
SqlCeCommand cmd = new SqlCeCommand(sql, connection);
SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds);
//datagridview1 is name of datagridview in form:
datagridview1.DataSource=ds.Tables[0];
connection.Close();
Try this.
string sql = "SELECT * FROM tablaMercancia";
connection.Open();
//SqlCeCommand cmd = new SqlCeCommand(sql, connection);
SqlCeDataAdapter da = new SqlCeDataAdapter(sql, connection);
DataSet ds=new DataSet();
da.Fill(ds);
designing page track the gridview or data grid
1st use namespace using System.Data,SqlClient;
sqlconnection con=new sqlconnection("string path");
con.open();
sqldataadapter da=new sqldataadapter("select * from emp",con);
dataset ds=new dataset();
da.fill(ds,"emp");
gridview1.datasource=ds;
gridview1.databind();

C# -> Retrieving dataset from SQL Server 2008

I have a table called NAMES in my SQL Server database. I am trying to retrieve the entire table and put it into a dataset:
//get the connection string from web.config
string connString = ConfigurationManager.ConnectionStrings["Platform"].ConnectionString;
DataSet dataset = new DataSet();
using (SqlConnection conn = new SqlConnection(connString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("NAMES", conn);
adapter.Fill(dataset);
}
This throws a sql exception though,
"Invalid Object Name NAMES"...
What am I doing wrong?
Open the connection !!!!!!
//get the connection string from web.config
string connString = ConfigurationManager .ConnectionStrings["Platform"].ConnectionString;
DataSet dataset = new DataSet();
using (SqlConnection conn = new SqlConnection(connString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("select * from [NAMES]", conn);
conn.Open();
adapter.Fill(dataset);
}
You're not passing an actual SQL select command to the SqlCommand constructor.
First check whether Select * from dbo.[Names] is wroking in ur sql ?
string connString = ConfigurationManager .ConnectionStrings["Platform"].ConnectionString;
Dataset ds=new Dataset();
SqlConnection con = new Sqlconnection(connString);
SqlDataAdapter adapter = new SqlDataAdapter("Select * from dbo.[Names]",con);
adapter.Fill(ds);

Categories