Add Datatable (multiple rows) to ms-access just once without Loop C# - c#

I have a datatable table which have multiple records. I want to Insert this datatable to ms-access table without using loop.
I want to insert multiple rows/records into ms-access database as whole. I don't want to insert records one by one.
using (var conn = new OleDbConnection(DatabaseObjects.ConnectionString))
{
var adap = new OleDbDataAdapter();
adap.SelectCommand = new OleDbCommand ("select RollNo, SName, FName, DOB, [Section] from students", conn);
var cb = new OleDbCommandBuilder(adap);
cb.GetInsertCommand();
cb.GetDeleteCommand();
cb.GetUpdateCommand();
conn.Open();
adap.Update(table);
}
Loading of data from excel sheet to datatable. code is below,
using (OleDbConnection connExcel = new OleDbConnection(DatabaseObjects.ConnectionStringExcel))
{
string queryExcel = "select * from [" + sheetName + "$]";
using (OleDbCommand commandExcel = new OleDbCommand(queryExcel, connExcel))
{
connExcel.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = commandExcel;
adapter.Fill(dtSheetData);
}
}

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 synchronise database with datatable

I want to syncronize my database with from my datagridview. When i modify, add or remove columns from my datagridview i want to update changed rows only.
Here is my code, but this code always insert new rows each time i run it so it has duplicate values.
string conStr = "Server=servername,1433;Database=dbname;User ID=userid;Password=password;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
public void UpdateDatabase(DataTable dtOldList, DataTable dtNewList)
{
string selectStatement = "SELECT * FROM Customers";
System.Data.DataTable dt = new System.Data.DataTable();
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter();
sqlDa.SelectCommand = new SqlCommand(selectStatement, conn);
SqlCommandBuilder cb = new SqlCommandBuilder(sqlDa);
dtOldList.Merge(dtNewList);
DataTable d3 = dtNewList.GetChanges();
sqlDa.Update(d3);
}

Inserting from Excel sheet to sqlserver?

I'm trying to insert specific columns from excel into my database on sql server and here's the following code:
constr = string.Format(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1;""", FilePath);
Econ = new OleDbConnection(constr);
SqlConnection con = new SqlConnection("Data Source=(local);Database='SimDevice_Stocks';Integrated Security= yes;");
Query = string.Format("Select [ORDER_SUB_TYPE],[ORDER_DATE],[ORDER_STATUS],[SIM_NETWORK_TYPE],[STORE_ID],[ROOT_PRODUCT_ATM_TYPE],[STC_ERP_SIM_ITEM],[STC_SALECO_ITEM_CODE] FROM [{0}] where ORDER_SUB_TYPE='Change SIM' and ORDER_SUB_TYPE='New'and ORDER_STATUS = 'Complete' and ROOT_PRODUCT_ATM_TYPE='Prepaid'", "Online_Inventory_Open_Points_re$");
OleDbCommand Ecom = new OleDbCommand(Query, Econ);
Econ.Open();
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
Econ.Close();
oda.Fill(ds);
DataTable Exceldt = ds.Tables[0];
//creating object of SqlBulkCopy
SqlBulkCopy objbulk = new SqlBulkCopy(con);
//assigning Destination table name
objbulk.DestinationTableName = "Temp_DailyInventory";
//Mapping Table column
objbulk.ColumnMappings.Add("ORDER_SUB_TYPE", "Order_Sub_Type");
objbulk.ColumnMappings.Add("Order_Date", "Order_Date");
objbulk.ColumnMappings.Add("ORDER_STATUS", "Order_Status");
objbulk.ColumnMappings.Add("SIM_NETWORK_TYPE", "Sim_Network_Type");
objbulk.ColumnMappings.Add("STORE_ID", "Store_ID");
objbulk.ColumnMappings.Add("ROOT_PRODUCT_ATM_TYPE", "Root_Product_Atm_Type");
objbulk.ColumnMappings.Add("STC_ERP_SIM_ITEM", "Stc_Erp_Sim_Item");
objbulk.ColumnMappings.Add("STC_SALECO_ITEM_CODE", "Stc_Saleco_Item_Code");
con.Open();
objbulk.WriteToServer(Exceldt);
con.Close();
so i've got 2 type of data in my excel sheet column of the Order_Sub_Type it got Change sim and New so i need to enter those 2 records but i'm not able to insert them as you can see in the above query i can either choose one to enter "Change Sim" or "New".
Thanks in advance

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

error when merging three tables

i'm having an error " Object reference not set to an instance of an object. "
// Define the ADO.NET objects.
SqlConnection con = new SqlConnection(connectionString);
string selectSQL = "SELECT * FROM tbl_lecturer_project";
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet dsPubs = new DataSet();
// Try to open database and read information.
try
{
con.Open();
adapter.Fill(dsPubs, "tbl_lecturer_project");
// This command is still linked to the data adapter.
cmd.CommandText = "SELECT * FROM tbl_student_project_choice";
adapter.Fill(dsPubs, "tbl_student_project_choice");
cmd.CommandText = "SELECT * FROM tbl_team";
adapter.Fill(dsPubs, "tbl_team");
DataRelation SCoiceLec = new DataRelation("SCoiceLec", dsPubs.Tables["tbl_lecturer_project"].Columns["lecturerProjectId"], dsPubs.Tables["student_project_choice"].Columns["choiceProjectId"]);
DataRelation SChoiceNTeam = new DataRelation("SChoiceNTeam",dsPubs.Tables["student_project_choice"].Columns["choiceGroupId"], dsPubs.Tables["tbl_team"].Columns["teamId"]);
please help. i want to retrieve data from all 3 tables.
There are a number of problems with your code. Here is one:
adapter.Fill(dsPubs, "tbl_lecturer_project");
should be
adapter.Fill(dsPubs);
I think what you want is this:
string selectSQL = #"SELECT * FROM tbl_lecturer_project;
SELECT * FROM tbl_student_project_choice;
SELECT * FROM tbl_team";
using(SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using(SqlCommand cmd = new SqlCommand(selectSQL, con))
{
using(SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
DataSet dsPubs = new DataSet();
adapter.Fill(dsPubs);
// use dataset.
}
}
}
The three tables will have the names Table, Table1, and Table2

Categories