| | |
Rows not inserting
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 1
Reputation:
Solved Threads: 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:
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
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:
c# Syntax (Toggle Plain Text)
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
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.
c# Syntax (Toggle Plain Text)
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); } }
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
![]() |
Similar Threads
- Example of Inserting and Retrieving data from xml file (ASP.NET)
- The operation has timed out error while inserting data through web service (IT Professionals' Lounge)
- Inserting data from one row and change one field (MySQL)
- Dynamically add rows in datagrid (ASP.NET)
- insert multiple rows in database (MS SQL)
- Inserting data in database (VB.NET)
- Problem inserting data into database (JSP)
- Inserting datas from two tables to third table with a command. is it possible (MySQL)
- Template Rows in DetailsView Control (ASP.NET)
- inserting a size (PHP)
Other Threads in the C# Forum
- Previous Thread: hot to get html from url requiring login
- Next Thread: Error: name 'fucntionname' does not exist...
| Thread Tools | Search this Thread |
.net access algorithm array asp barchart bitmap box broadcast buttons c# check checkbox client column combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development display draganddrop drawing encryption enum equation event excel file form format formbox forms formupdate function gdi+ httpwebrequest image index input install java label linux list listbox mandelbrot math mouseclick mysql networking operator packaging parse path photoshop picturebox pixelinversion post powerpacks programming radians regex remote remoting reporting richtextbox robot server sleep socket sql statistics stream string table text textbox thread time timer transform treeview update usercontrol validation visualstudio webbrowser wfa windows winforms wpf xml






