I need to insert without any parameters and execution query.
Since, I have bulk of data's in a dataset the amount of transaction makes the application delay. So i need to directly insert my dataset to database table.
Since, I have bulk of data's in a dataset the amount of transaction makes the application delay. So i need to directly insert my dataset to database table.
Code:
DataSet FilecontentDataset = new DataSet();
String fileName = string.Empty;
try
{
string full = Path.GetFullPath(PathtoTextFile.PostedFile.FileName);
string file = Path.GetFileName(PathtoTextFile.PostedFile.FileName);
string dir = Path.GetDirectoryName(PathtoTextFile.PostedFile.FileName);
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=\"" + dir + "\\\";" + "Extended Properties=\"text;HDR=YES;FMT=Delimited\"";
//create the database query
string query = "SELECT * FROM " + file;
//create a DataTable to hold the query results
DataTable dTable = new DataTable();
//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
dAdapter.Fill(FilecontentDataset, "CSV");
SqlConnection con = OPENSQLCONNECTION();
con.Open();
//foreach (DataRow dbNewRow in FilecontentDataset.Tables[0].Rows)
//{
// SqlCommand cmd_SubmitEmployeeDetail = new SqlCommand("LoadCSVDetails", con);
// cmd_SubmitEmployeeDetail.CommandType = CommandType.StoredProcedure;
// cmd_SubmitEmployeeDetail.Parameters.Add("@UserName", SqlDbType.VarChar).Value = dbNewRow.ItemArray[0].ToString();
// cmd_SubmitEmployeeDetail.Parameters.Add("@Password", SqlDbType.VarChar).Value = dbNewRow.ItemArray[1].ToString();
// cmd_SubmitEmployeeDetail.Parameters.Add("@FullName", SqlDbType.VarChar).Value = dbNewRow.ItemArray[2].ToString();
// cmd_SubmitEmployeeDetail.Parameters.Add("@Restrictionlistname", SqlDbType.VarChar).Value = dbNewRow.ItemArray[3].ToString();
// cmd_SubmitEmployeeDetail.Parameters.Add("@Bannedwordchecking", SqlDbType.VarChar).Value = dbNewRow.ItemArray[4].ToString();
// cmd_SubmitEmployeeDetail.Parameters.Add("@Allowattachments", SqlDbType.VarChar).Value = dbNewRow.ItemArray[5].ToString();
// cmd_SubmitEmployeeDetail.Parameters.Add("@Template", SqlDbType.VarChar).Value = dbNewRow.ItemArray[6].ToString();
// cmd_SubmitEmployeeDetail.Parameters.Add("@Group1", SqlDbType.VarChar).Value = dbNewRow.ItemArray[7].ToString();
// cmd_SubmitEmployeeDetail.Parameters.Add("@Mailboxsize", SqlDbType.VarChar).Value = dbNewRow.ItemArray[8].ToString();
// cmd_SubmitEmployeeDetail.Parameters.Add("@Calendar", SqlDbType.VarChar).Value = dbNewRow.ItemArray[9].ToString();
// cmd_SubmitEmployeeDetail.Parameters.Add("@SyncML", SqlDbType.VarChar).Value = dbNewRow.ItemArray[10].ToString();
// int i = cmd_SubmitEmployeeDetail.ExecuteNonQuery();
//}
//con.Close();
//SqlDataAdapter adapter = new SqlDataAdapter();
//// A table mapping names the DataTable.
//adapter.TableMappings.Add("Table","CSVLoader");
//// Open the connection.
//con.Open();
//GridView1.DataSource = FilecontentDataset.Tables[0];
//GridView1.DataBind();
// // Create a SqlCommand to retrieve Suppliers data.
SqlCommand command = new SqlCommand("select * from CSVLoader", con);
// command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
int i = command.ExecuteNonQuery();
DataSet DB = new DataSet();
adapter.Fill(DB);
// Set the SqlDataAdapter's SelectCommand.
adapter.InsertCommand = command;
// Fill the DataSet.
// DataSet dataSet = new DataSet("Suppliers");
// adapter.Fill(FilecontentDataset);
adapter.Update(FilecontentDataset, "CSV");
// SqlDataAdapter da = new SqlDataAdapter("Insert into CSVLoader (UserName, Password)values(6,6)", con);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
DataSet ds = new DataSet();
// da.Fill(FilecontentDataset, "CSVLoader");
// da.Update("CSVLoader","CSV");
con.Close();
SqlDataAdapter myDataAdapter = new SqlDataAdapter();
myDataAdapter.InsertCommand = new SqlCommand("insert into CSVLoader select * from " + FilecontentDataset.Tables[0] + "" , con);
SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);
// myDataAdapter.Update(FilecontentDataset.Tables["CSV"],"CSVLoader");
myDataAdapter.SelectCommand = new SqlCommand("select * from CSVLOader", con);
DataSet ds = new DataSet();
myDataAdapter.Fill(ds, "myTableName");
// ds.Tables.Clear();
// ds.Tables[0]=(FilecontentDataset.Tables[0]);
//code to modify data in DataSet here
//Without the SqlCommandBuilder this line would fail
cb.GetInsertCommand();
myDataAdapter.Update(FilecontentDataset.Tables[0]);
// return ds;
}
catch (Exception ex)
{
Label2.Text = ex.Message.ToString();
}
Comment