Adapter not loading anything with SQLite C# - c#

I use this code and SQLite says "Data Source cannot be empty. Use :memory: to open an in-memory database"
here is my code
string dbfile = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\m23.db";
string sql;
DateTime dt = DateTime.Now;
SQLiteConnection connection = new SQLiteConnection("datasource="+ dbfile);
SQLiteDataAdapter adapter = new SQLiteDataAdapter("select * from p_posts", connection);
DataSet data = new DataSet();
adapter.Fill(data); // <<< here i get the error
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandType = CommandType.TableDirect;
cmd.Connection = connection;
connection.Open();
Any help is apreciated ,
Cheers !

Try using
SQLiteConnection connection = new SQLiteConnection("Data Source=" + dbfile);
The examples I've seen use "Data Source" instead of "datasource".

To avoid such parameter syntax issues you might also consider using SQLiteConnectionStringBuilder
SQLiteConnectionStringBuilder con = new SQLiteConnectionStringBuilder();
con.DataSource = dbfile;

Related

From the database I am trying to display a single common name using label please guide me I am a beginner in C#

I am trying to make a feedback form where I want to show the name which is inserted n number of times.
My DataBase has for example 9 duplicate names as feedback was input for that same person 9 times and I want to display it on the result that common name.
Please help me out to complete the code/solution or Correct the code and get the result.
SQL QUERY IS RUNNING PROPERLY IT IS SELECTING THE SINGLE DATA FROM DATABASE BUT HOW TO SHOW THIS ON WEBPAGE
public void cal_F2name()
{
string oracledb = "Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP****))(****))(CONNECT_DATA =(SERVER = DEDICATED)(SID = ORCL));";
OracleConnection conn = new OracleConnection(oracledb);
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
OracleDataAdapter da1 = new OracleDataAdapter();
DataTable dt1 = new DataTable();
DataSet ds1 = new DataSet();
cmd.CommandText = "SELECT DISTINCT (F2NAME) FROM CMDC_FEEDBACK WHERE PRG_NAME ='" + cb_prg_name.SelectedValue + "'";
da1.SelectCommand = cmd;
da1.Fill(ds1);
name = Convert.ToString(ds1.Tables[0].Rows[0][0].ToString());
Label58.Text = String.Format("{0:0.00}",name);
conn.Close();
}
try using below code, i have made some change in sql query to get only single record as result.
public void cal_F2name()
{
string oracledb = "Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP****))(****))(CONNECT_DATA =(SERVER = DEDICATED)(SID = ORCL));";
OracleConnection conn = new OracleConnection(oracledb);
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
OracleDataAdapter da1 = new OracleDataAdapter();
DataTable dt1 = new DataTable();
DataSet ds1 = new DataSet();
cmd.CommandText = "SELECT max(DISTINCT (F2NAME)) FROM CMDC_FEEDBACK WHERE PRG_NAME ='" + cb_prg_name.SelectedValue + "' AND F2NAME<>'' AND F2NAME IS NOT NULL" ;
da1.SelectCommand = cmd;
da1.Fill(ds1);
name = Convert.ToString(ds1.Tables[0].Rows[0][0].ToString());
Label58.Text = String.Format("{0:0.00}",name);
conn.Close();
}
i have not check but it will work, if you result binding to lable is correct.

How to convert Ods file to Data Set C#

I am unable to convert ods file to data set
I have downloaded the file from follwoing link
UK List ods file link
and using following piece of code to convert this ods file to data set
int count = 0;
int list_clear = 1;
string connectionString;
if (ConfigurationManager.AppSettings["OperatingSystem"] == "64Bits")
connectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + location + ";Extended Properties=\"Excel 12.0;\"";
else
connectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
location + #";Extended Properties=""Excel 8.0;HDR=YES;""";
int recCount = 0;
string xmlData = "";
// Create a factory
DbProviderFactory factory =
DbProviderFactories.GetFactory("System.Data.OleDb");
// Create an adapter
DbDataAdapter adapter = factory.CreateDataAdapter();
// Create a select command to get the dataset
DbCommand selectCommand = factory.CreateCommand();
//selectCommand.CommandText = "SELECT * FROM [sanctionsconlist$]";
selectCommand.CommandText = "SELECT * FROM [sheet1$]";// "SELECT * FROM [Data$]";
// Set up the connection and the connection string
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = connectionString;
selectCommand.Connection = connection;
// Set up the command
adapter.SelectCommand = selectCommand;
// Create the dataset
DataSet ds = new DataSet();
DataSet dsData = new DataSet();
// Load the results
adapter.Fill(ds);
con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStringName"].ConnectionString);
cmd = new SqlCommand();
con.Open();
tran = con.BeginTransaction();
cmd.CommandText = "USP_ImportUKListSanctionListData";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.Transaction = tran;
cmd.Parameters.Add("#xmlData", SqlDbType.NVarChar, -1);
cmd.Parameters.Add("#list_clear_flag", SqlDbType.Int);
cmd.Parameters.Add("#update_list", SqlDbType.Int);
cmd.Parameters.Add("#Error", SqlDbType.Int);
DataTable dt = ds.Tables[0].Clone();
but i get the error, external table is not in the expected format
Can any one help me in this reagard, thanks

Oracle Data Provider for .Net - No data querying

I am using Oracle Data Provider for .NET Assembly to query Oracle database.
Below is my code.
Though the code runs with no error. I don’t get data to my dataset dsOracleData.
However, when I run the query after connecting to SQL Developer, I can see the resulting data for the query.
OracleConnection conn = new OracleConnection(“ConnectionString”)
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = Query1.ToString();
cmd.CommandType = CommandType.Text;
OracleDataAdapter adapter = new OracleDataAdapter(cmd);
OracleCommandBuilder builder = new OracleCommandBuilder(adapter);
DataSet dsOracleData = new DataSet();
adapter.Fill(dsOracleData);
Hi u can try like this,
OracleConnection conn = new OracleConnection("Your Connection string");
Conn.Open;
DataSet dataSet = new DataSet();
OracleCommand cmd = new OracleCommand("your select query");
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
using (OracleDataAdapter dataAdapter = new OracleDataAdapter())
{
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dataSet);
}

How to View Data from MS Access in Data Grid View?

I am new in C-Sharp, i am trying to access my Database from C-Sharp, i have written the following code, and i dont know what to write next to view data. I have searched this on net but didnt get much. Kindly tell me this in easy code.
string connection = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Database3.accdb";
OleDbConnection conn = new OleDbConnection(connection);
conn.Open();
OleDbCommand cmd = new OleDbCommand("Select * from score", conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.SelectCommand = cmd;
Refer following code:
string strProvider = "#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Database3.accdb";
string strSql = "Select * from score";
OleDbConnection con = new OleDbConnection(strProvider);
OleDbCommand cmd = new OleDbCommand(strSql, con);
con.Open();
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable scores = new DataTable();
da.Fill(scores);
dataGridView1.DataSource = scores;
Hope its helpful.
Try this
try
{
Dataset myDataSet=new Dataset();
string connection = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Database3.accdb";
OleDbCommand cmd = new OleDbCommand("Select * from score", conn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(cmd );
connection .Open();
myDataAdapter.Fill(myDataSet,"TableName");
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
return;
}
finally
{
connection .Close();
}
Remember for good coding practice ,always connection should be open in Try Block
and closed in Finally Block

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();

Categories