Israel

Israel

  • 1.1k
  • 1.3k
  • 177.9k

Same code for MSACCESS and SQLSERVER but giving error

Dec 30 2019 7:32 AM
Hi, as you see I have two pieces of codes transfering datas Table1 to Table2.
 
1) The first piece is transfering datas using MSAccess’s Table. It doing well the job.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows.Forms;  
  6. namespace WindowsFormsApplication1  
  7. {  
  8. public partial class Form1 : Form  
  9. {  
  10. public Form1()  
  11. {  
  12. InitializeComponent();  
  13. }  
  14. private void button1_Click(object sender, EventArgs e)  
  15. {  
  16. string myConnectionString;  
  17. myConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Copy_table_to_table\WindowsFormsApplication1\App_Datas\test.mdb;Persist Security Info=False";  
  18. using (var con = new OleDbConnection())  
  19. {  
  20. con.ConnectionString = myConnectionString;  
  21. con.Open();  
  22. using (var cmd = new OleDbCommand())  
  23. {  
  24. cmd.Connection = con;  
  25. cmd.CommandType = System.Data.CommandType.Text;  
  26. cmd.CommandText = @"INSERT INTO test2 IN 'C:\Copy_table_to_table\WindowsFormsApplication1\App_Datas\test.mdb' " + @"SELECT A,B,C FROM test1";  
  27. cmd.ExecuteNonQuery();  
  28. }  
  29. con.Close();  
  30. }  
  31. Console.WriteLine("Done.");  
  32. }  
  33. }  
  34. }  
2) The Segond piece is transfering from Table1 to Table2 uing SQLSERVER’s Table. Unfortunatly, its stucking giving this error message: Incorrect syntax near the keyword 'IN'
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows.Forms;  
  6. using System.Data;  
  7. using System.Data.SqlClient;  
  8. namespace WindowsFormsApplication1  
  9. {  
  10. public partial class Form1 : Form  
  11. {  
  12. public Form1()  
  13. {  
  14. InitializeComponent();  
  15. }  
  16. SqlConnection conn = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\Copy_table_to_table\\WindowsFormsApplication1\\test.mdf;Integrated Security = True;Integrated Security = True");  
  17. SqlCommand comm;  
  18. SqlDataAdapter da;  
  19. string connstr = @"Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\Copy_table_to_table\\WindowsFormsApplication1\\test.mdf;Integrated Security = True;Integrated Security = True";  
  20. private void button1_Click(object sender, EventArgs e)  
  21. {  
  22. string myConnectionString;  
  23. myConnectionString = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = C:\\Copy_table_to_table\\WindowsFormsApplication1\\test.mdf; Integrated Security = True; Integrated Security = True";  
  24. {  
  25. conn = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = C:\\Copy_table_to_table\\WindowsFormsApplication1\\test.mdf; Integrated Security = True; Integrated Security = True");  
  26. if (conn.State != ConnectionState.Open)  
  27. conn.Open();  
  28. using (var cmd = new SqlCommand())  
  29. {  
  30. cmd.Connection = conn;  
  31. cmd.CommandType = System.Data.CommandType.Text;  
  32. cmd.CommandText = @"INSERT INTO test2 IN 'C:\Copy_table_to_table\WindowsFormsApplication1\test.mdf' " + @"SELECT A,B,C FROM test1";  
  33. cmd.ExecuteNonQuery();  
  34. }  
  35. }  
  36. Console.WriteLine("Done.");  
  37. conn.Close();  
  38. }  
  39. }  
  40. }  

Answers (1)