473,699 Members | 2,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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 22756
haarigee
2 New Member
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 Contributor
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 Recognized Expert Specialist
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
1883
by: wijndrinker | last post by:
is there some standard file(txt) to to this? or is there an existing php object?
2
3327
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 can't specify target table 'student' for update in FROM clause mysql>
1
1348
by: srikarn | last post by:
how to retrieve randomly rows from a sqlserver database table
7
2425
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 inserting except checkbox value, i dont know in sql server 2005 which data type i have to use for that column to insert the checkbox checked values please reply me its very urgent
2
1290
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
3524
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 in the database if anybody knows plz help me... sql server2005 server type: database Engine authentication :windows authentication here is my code private void butinsert_Click(object sender, EventArgs e) {
3
2060
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 and add 3 more rows. but not able to update that dataset to database table. My code is following. SqlDataAdapter SDA = new SqlDataAdapter("Select * from WFF", DAC.Conn); SDA.Fill(dt,"WFF"); //Updates dt here SDA.Update(dt); It does...
4
1399
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 steps if any one can solve the following example which is not the same project which im doing.... here if suppose i have my web page with lables EmployeeNumber, EmployeeName,EmployeeAddress,DepartmentName,DepartmentLocation,Salary,...
5
4824
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 time. I want to save these changes in the table without adding new rows, just want to replace old values with new ones. I'v used the "adapter.Update" command but it does not work. Plz reply me thankx in advance
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.