473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to insert values from dataset or datatable to database table in C#.net

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.

Expand|Select|Wrap|Line Numbers
  1. DataSet FilecontentDataset = new DataSet();
  2.         String fileName = string.Empty;
  3.  
  4.         try
  5.         {
  6.             string full = Path.GetFullPath(PathtoTextFile.PostedFile.FileName);
  7.             string file = Path.GetFileName(PathtoTextFile.PostedFile.FileName);
  8.             string dir = Path.GetDirectoryName(PathtoTextFile.PostedFile.FileName);
  9.  
  10.             string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=\"" + dir + "\\\";"    + "Extended Properties=\"text;HDR=YES;FMT=Delimited\"";
  11.  
  12.             //create the database query
  13.             string query = "SELECT * FROM " + file;
  14.  
  15.             //create a DataTable to hold the query results
  16.             DataTable dTable = new DataTable();
  17.  
  18.             //create an OleDbDataAdapter to execute the query
  19.             OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
  20.             dAdapter.Fill(FilecontentDataset, "CSV");
  21.  
  22.             SqlConnection con = OPENSQLCONNECTION();
  23.             con.Open();
  24.  
  25.  
  26.             //foreach (DataRow dbNewRow in FilecontentDataset.Tables[0].Rows)
  27.             //{
  28.             //    SqlCommand cmd_SubmitEmployeeDetail = new SqlCommand("LoadCSVDetails", con);
  29.             //    cmd_SubmitEmployeeDetail.CommandType = CommandType.StoredProcedure;
  30.  
  31.  
  32.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@UserName", SqlDbType.VarChar).Value = dbNewRow.ItemArray[0].ToString();
  33.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Password", SqlDbType.VarChar).Value = dbNewRow.ItemArray[1].ToString();
  34.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@FullName", SqlDbType.VarChar).Value = dbNewRow.ItemArray[2].ToString();
  35.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Restrictionlistname", SqlDbType.VarChar).Value = dbNewRow.ItemArray[3].ToString();
  36.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Bannedwordchecking", SqlDbType.VarChar).Value = dbNewRow.ItemArray[4].ToString();
  37.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Allowattachments", SqlDbType.VarChar).Value = dbNewRow.ItemArray[5].ToString();
  38.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Template", SqlDbType.VarChar).Value = dbNewRow.ItemArray[6].ToString();
  39.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Group1", SqlDbType.VarChar).Value = dbNewRow.ItemArray[7].ToString();
  40.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Mailboxsize", SqlDbType.VarChar).Value = dbNewRow.ItemArray[8].ToString();
  41.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Calendar", SqlDbType.VarChar).Value = dbNewRow.ItemArray[9].ToString();
  42.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@SyncML", SqlDbType.VarChar).Value = dbNewRow.ItemArray[10].ToString();
  43.             //    int i = cmd_SubmitEmployeeDetail.ExecuteNonQuery();
  44.             //}
  45.  
  46.  
  47.             //con.Close();
  48.  
  49.             //SqlDataAdapter adapter = new SqlDataAdapter();
  50.  
  51.             //// A table mapping names the DataTable.
  52.             //adapter.TableMappings.Add("Table","CSVLoader");
  53.  
  54.             //// Open the connection.
  55.             //con.Open();
  56.  
  57.             //GridView1.DataSource = FilecontentDataset.Tables[0];
  58.             //GridView1.DataBind();
  59.  
  60.          //   // Create a SqlCommand to retrieve Suppliers data.
  61.             SqlCommand command = new SqlCommand("select * from CSVLoader", con);
  62.             //  command.CommandType = CommandType.Text;
  63.  
  64.             adapter.SelectCommand = command;
  65.             int i = command.ExecuteNonQuery();
  66.             DataSet DB = new DataSet();
  67.             adapter.Fill(DB);
  68.  
  69.             // Set the SqlDataAdapter's SelectCommand.
  70.             adapter.InsertCommand = command;
  71.  
  72.             // Fill the DataSet.
  73.             // DataSet dataSet = new DataSet("Suppliers");
  74.             // adapter.Fill(FilecontentDataset);
  75.             adapter.Update(FilecontentDataset, "CSV");
  76.  
  77.  
  78.             //   SqlDataAdapter da = new SqlDataAdapter("Insert into CSVLoader (UserName, Password)values(6,6)", con);
  79.             SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
  80.             DataSet ds = new DataSet();
  81.             // da.Fill(FilecontentDataset, "CSVLoader");
  82.             //   da.Update("CSVLoader","CSV");
  83.             con.Close();
  84.  
  85.  
  86.             SqlDataAdapter myDataAdapter = new SqlDataAdapter();
  87.             myDataAdapter.InsertCommand = new SqlCommand("insert into CSVLoader select * from " + FilecontentDataset.Tables[0] + "" , con);
  88.             SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);
  89.  
  90.           //  myDataAdapter.Update(FilecontentDataset.Tables["CSV"],"CSVLoader");
  91.             myDataAdapter.SelectCommand = new SqlCommand("select * from CSVLOader", con);
  92.             DataSet ds = new DataSet();
  93.  
  94.             myDataAdapter.Fill(ds, "myTableName");
  95.  
  96.           //  ds.Tables.Clear();
  97.            // ds.Tables[0]=(FilecontentDataset.Tables[0]);
  98.  
  99.             //code to modify data in DataSet here
  100.  
  101.             //Without the SqlCommandBuilder this line would fail
  102.             cb.GetInsertCommand();
  103.             myDataAdapter.Update(FilecontentDataset.Tables[0]);
  104.  
  105.  
  106.            // return ds;                                 
  107.         }
  108.         catch (Exception ex)
  109.         {
  110.             Label2.Text = ex.Message.ToString();
  111.  
  112.         }
Jan 21 '10 #1
4 22725
Expand|Select|Wrap|Line Numbers
  1. DataSet FilecontentDataset = new DataSet();
  2.         String fileName = string.Empty;
  3.  
  4.         try
  5.         {
  6.             string full = Path.GetFullPath(PathtoTextFile.PostedFile.FileName);
  7.             string file = Path.GetFileName(PathtoTextFile.PostedFile.FileName);
  8.             string dir = Path.GetDirectoryName(PathtoTextFile.PostedFile.FileName);
  9.  
  10.             string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=\"" + dir + "\\\";"    + "Extended Properties=\"text;HDR=YES;FMT=Delimited\"";
  11.  
  12.             //create the database query
  13.             string query = "SELECT * FROM " + file;
  14.  
  15.             //create a DataTable to hold the query results
  16.             DataTable dTable = new DataTable();
  17.  
  18.             //create an OleDbDataAdapter to execute the query
  19.             OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
  20.             dAdapter.Fill(FilecontentDataset, "CSV");
  21.  
  22.             SqlConnection con = OPENSQLCONNECTION();
  23.             con.Open();
  24.  
  25.  
  26.             //foreach (DataRow dbNewRow in FilecontentDataset.Tables[0].Rows)
  27.             //{
  28.             //    SqlCommand cmd_SubmitEmployeeDetail = new SqlCommand("LoadCSVDetails", con);
  29.             //    cmd_SubmitEmployeeDetail.CommandType = CommandType.StoredProcedure;
  30.  
  31.  
  32.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@UserName", SqlDbType.VarChar).Value = dbNewRow.ItemArray[0].ToString();
  33.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Password", SqlDbType.VarChar).Value = dbNewRow.ItemArray[1].ToString();
  34.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@FullName", SqlDbType.VarChar).Value = dbNewRow.ItemArray[2].ToString();
  35.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Restrictionlistname", SqlDbType.VarChar).Value = dbNewRow.ItemArray[3].ToString();
  36.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Bannedwordchecking", SqlDbType.VarChar).Value = dbNewRow.ItemArray[4].ToString();
  37.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Allowattachments", SqlDbType.VarChar).Value = dbNewRow.ItemArray[5].ToString();
  38.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Template", SqlDbType.VarChar).Value = dbNewRow.ItemArray[6].ToString();
  39.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Group1", SqlDbType.VarChar).Value = dbNewRow.ItemArray[7].ToString();
  40.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Mailboxsize", SqlDbType.VarChar).Value = dbNewRow.ItemArray[8].ToString();
  41.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@Calendar", SqlDbType.VarChar).Value = dbNewRow.ItemArray[9].ToString();
  42.             //    cmd_SubmitEmployeeDetail.Parameters.Add("@SyncML", SqlDbType.VarChar).Value = dbNewRow.ItemArray[10].ToString();
  43.             //    int i = cmd_SubmitEmployeeDetail.ExecuteNonQuery();
  44.             //}
  45.  
  46.  
  47.             //con.Close();
  48.  
  49.             //SqlDataAdapter adapter = new SqlDataAdapter();
  50.  
  51.             //// A table mapping names the DataTable.
  52.             //adapter.TableMappings.Add("Table","CSVLoader");
  53.  
  54.             //// Open the connection.
  55.             //con.Open();
  56.  
  57.             //GridView1.DataSource = FilecontentDataset.Tables[0];
  58.             //GridView1.DataBind();
  59.  
  60.          //   // Create a SqlCommand to retrieve Suppliers data.
  61.             SqlCommand command = new SqlCommand("select * from CSVLoader", con);
  62.             //  command.CommandType = CommandType.Text;
  63.  
  64.             adapter.SelectCommand = command;
  65.             int i = command.ExecuteNonQuery();
  66.             DataSet DB = new DataSet();
  67.             adapter.Fill(DB);
  68.  
  69.             // Set the SqlDataAdapter's SelectCommand.
  70.             adapter.InsertCommand = command;
  71.  
  72.             // Fill the DataSet.
  73.             // DataSet dataSet = new DataSet("Suppliers");
  74.             // adapter.Fill(FilecontentDataset);
  75.             adapter.Update(FilecontentDataset, "CSV");
  76.  
  77.  
  78.             //   SqlDataAdapter da = new SqlDataAdapter("Insert into CSVLoader (UserName, Password)values(6,6)", con);
  79.             SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
  80.             DataSet ds = new DataSet();
  81.             // da.Fill(FilecontentDataset, "CSVLoader");
  82.             //   da.Update("CSVLoader","CSV");
  83.             con.Close();
  84.  
  85.  
  86.             SqlDataAdapter myDataAdapter = new SqlDataAdapter();
  87.             myDataAdapter.InsertCommand = new SqlCommand("insert into CSVLoader select * from " + FilecontentDataset.Tables[0] + "" , con);
  88.             SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);
  89.  
  90.           //  myDataAdapter.Update(FilecontentDataset.Tables["CSV"],"CSVLoader");
  91.             myDataAdapter.SelectCommand = new SqlCommand("select * from CSVLOader", con);
  92.             DataSet ds = new DataSet();
  93.  
  94.             myDataAdapter.Fill(ds, "myTableName");
  95.  
  96.           //  ds.Tables.Clear();
  97.            // ds.Tables[0]=(FilecontentDataset.Tables[0]);
  98.  
  99.             //code to modify data in DataSet here
  100.  
  101.             //Without the SqlCommandBuilder this line would fail
  102.             cb.GetInsertCommand();
  103.             myDataAdapter.Update(FilecontentDataset.Tables[0]);
  104.  
  105.  
  106.            // return ds;                                 
  107.         }
  108.         catch (Exception ex)
  109.         {
  110.             Label2.Text = ex.Message.ToString();
  111.  
  112.         }
Jan 21 '10 #2
madankarmukta
308 256MB
Hi,

Check whether you followed these steps

1) create the connection obj
2) create the command object; assign the connection to this command obj
3) assign the command text as the name of the stored procedure, add the input parameters to the command object.
There Input parameters will be the values u wann to insert to the DB table.
4) create the Stored procedure which will insert the provided input values to the required table.
5) Call the ExecuteNonQuery and get your work done.

Hope this will help!

Thanks!
Jan 21 '10 #3
tlhintoq
3,525 Expert 2GB
Please don't double post your messages.

This makes it hard to coordinate efforts to help you.

The two threads have been merged
Jan 21 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: wijndrinker | last post by:
is there some standard file(txt) to to this? or is there an existing php object?
2
by: phillip.s.powell | last post by:
This is what I tried: update student_db.student set activities = (select i.activities from client.student c, student_db.student s where c.unique_key = s.unique_key); ERROR 1093 (HY000): You...
1
by: srikarn | last post by:
how to retrieve randomly rows from a sqlserver database table
7
by: bhargavigupta | last post by:
hi, In my application i have one checkbox when ever i am entering values to the page it has to store the values into the database table using insert statement , remaining values r...
2
by: jeenajos | last post by:
Hi all, while writing a query to insert values into a database it gave me an error. Query goes like this: Insert into .. ( , ,) Values (<Cargo,Varchar(50),>
1
by: rekhasc | last post by:
i am using the database SQLSERver 2005, i m new to this i worked in sql server2000... but using sqlserver 2005 i dont know how to insert the values... i write the code but the values is not storing...
3
by: sachinkale123 | last post by:
I am using SQL Adapter to select data from a table in SQL Server Database. After creating Object I am filling that to a datatable. Assum that dataset has 5 row. After that I m updating that dataset...
4
by: satyabhaskar | last post by:
I need ur help, Actually im designing a web page for my project in ASP.NET with C#....I got Struck in inserting data into the database from the web page.... I feel i can take forward...
5
by: asmi | last post by:
I am new in C# programming. Can anyone plz help me in the following problem. I am changing values in different columns in access database table using c# but these changes are only visible at run...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.