Rows not inserting

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 1
Reputation: jfoltz is an unknown quantity at this point 
Solved Threads: 0
jfoltz jfoltz is offline Offline
Newbie Poster

Rows not inserting

 
0
  #1
Mar 18th, 2008
Greetings,

I have a perplexing problem. I'm writing a small C# Application. I need to insert an file path into a database table. I'm using SQL Server 2005. This is the first time I've done much with SQL Server (I have had experience with other DB platforms though), so please bear with me.

My problem is that the command executes the NonQuery (the insert statement) and doesn't throw any errors. Yet, the rows do not show up in the database. I'm using SQL Server Management Studio Express as my GUI tool.

Here is my code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Data.SqlClient;
  11. using System.Data.Sql;
  12.  
  13. namespace Senior_Research
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. /*
  23.   * Connect to the SQL Server 2005 Database
  24.   */
  25. private SqlConnection ConnectToDatabase()
  26. {
  27. string ConnStrng = "Data Source=TOSHIBA\\SQLEXPRESS;Initial Catalog=SeniorResearch;Trusted_Connection=yes";
  28.  
  29. try
  30. {
  31. SqlConnection thisConnection = new SqlConnection(ConnStrng);
  32. thisConnection.Open();
  33. return thisConnection;
  34. }
  35. catch (SqlException e)
  36. {
  37. Console.WriteLine(e.Message);
  38. return null;
  39. }
  40. }
  41.  
  42. private void executeQuery(String sql, SqlConnection thisConnection)
  43. {
  44. SqlCommand thisCommand;
  45. SqlTransaction trans;
  46.  
  47. try
  48. {
  49. trans = thisConnection.BeginTransaction();
  50. thisCommand = new SqlCommand(sql, thisConnection,trans);
  51. thisCommand.CommandText = sql;
  52. thisCommand.ExecuteNonQuery();
  53. }
  54. catch (Exception e)
  55. {
  56. Console.WriteLine(e.Message);
  57. }
  58. }
  59.  
  60. private void closeConn(SqlConnection conn)
  61. {
  62. conn.Close();
  63. }
  64.  
  65. private void loadDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
  66. {
  67. String sql = null;
  68. DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
  69. FileInfo[] fi = di.GetFiles();
  70.  
  71. foreach (FileInfo fiTemp in fi)
  72. {
  73. //Connect to Database
  74. SqlConnection conn = ConnectToDatabase();
  75. //sql = "Insert into Images(file_name) values ('" + fiTemp.FullName + "')";
  76. sql = "Insert into Images(file_name) values ('test')";
  77. executeQuery(sql, conn);
  78. closeConn(conn);
  79. }
  80. }
  81. }
  82.  
  83. }

I'm not sure what the cause of the problem is, but it's probably something small I'm over looking. Any help, suggestions or direction would be greatly appreciated.

Thanks,
Jeremy
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Rows not inserting

 
0
  #2
Mar 18th, 2008
You may be in need to CommitTransaction??! I think so I don't have Visual Studio right now. if you don't make transaction it'll work well, but working with Transaction need you to commit the update.

  1. private void executeQuery(String sql, SqlConnection thisConnection)
  2. {
  3. SqlCommand thisCommand;
  4. try
  5. {
  6. thisCommand = new SqlCommand(sql, thisConnection);
  7. thisCommand.CommandText = sql;
  8. thisCommand.ExecuteNonQuery();
  9. }
  10. catch (Exception e)
  11. {
  12. Console.WriteLine(e.Message);
  13. }
  14. }
Last edited by Ramy Mahrous; Mar 18th, 2008 at 11:48 pm.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC