0

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:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Data.Sql;

namespace Senior_Research
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /*
         * Connect to the SQL Server 2005 Database
         */
        private SqlConnection ConnectToDatabase()
        {
            string ConnStrng = "Data Source=TOSHIBA\\SQLEXPRESS;Initial Catalog=SeniorResearch;Trusted_Connection=yes";

            try
			{
		    	SqlConnection thisConnection = new SqlConnection(ConnStrng);
                thisConnection.Open();
                return thisConnection;
			}
			catch (SqlException e)
			{
                Console.WriteLine(e.Message);
                return null;
			}
		}

        private void executeQuery(String sql, SqlConnection thisConnection)
        {
            SqlCommand thisCommand;
            SqlTransaction trans;

            try
            {
                trans = thisConnection.BeginTransaction();
                thisCommand = new SqlCommand(sql, thisConnection,trans);
                thisCommand.CommandText = sql;
                thisCommand.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        private void closeConn(SqlConnection conn)
        {
            conn.Close();
        }

        private void loadDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String sql = null;
            DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
            FileInfo[] fi = di.GetFiles();

            foreach (FileInfo fiTemp in fi)
            {
                //Connect to Database
                SqlConnection conn = ConnectToDatabase();
                //sql = "Insert into Images(file_name) values ('" + fiTemp.FullName + "')";
                sql = "Insert into Images(file_name) values ('test')";
                executeQuery(sql, conn);
                closeConn(conn);
            }
        }
	}

        }

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

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Ramy Mahrous
0

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.

private void executeQuery(String sql, SqlConnection thisConnection)        
{            
SqlCommand thisCommand;                         
try            
{                                
thisCommand = new SqlCommand(sql, thisConnection);                
thisCommand.CommandText = sql;                
thisCommand.ExecuteNonQuery();            
}            
catch (Exception e)            
{                
Console.WriteLine(e.Message);            
}        
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.