Groups | Blog | Home
all groups > sql server programming > may 2007 >

sql server programming : simple insert sql statement does'nt work!


giddy
5/5/2007 4:27:28 PM
hi ,

Ok , i'm tried everything , but this does'nt work:

class DAL {
public static void RunQuery(string sqlStr)
{
using (SqlConnection connection = new
SqlConnection(connStr))
{
SqlCommand cmd = new SqlCommand(sqlStr,connection);
connection.Open();
cmd.ExecuteNonQuery();
}
}
}

When i do ths:
DAL.runQuery("INSERT INTO Persons ('FirstName','LastName',......)
VALUES ("Gideon','Dsouza'.....)");

Theres no exception , nothing. Just nothing happens , it runs and ends
quietly , but **i dont see the data in the Database!!!?**

I have multiple projects in my solution. The DB is in a folder within
the _core_ class lib. project folder. I make this above call in
another (console test) project. Theres a copy of , the .dll and the
Database within that project folder.Is this causing problems?

Could someone please help me!

Gideon
Mike C#
5/5/2007 8:49:21 PM
Try this

DAL.runQuery("INSERT INTO Persons (FirstName, LastName ,......) /*Get rid of
the single quotes here */ " +
" VALUES ('Gideon','Dsouza'.....) /* Get rid of the double quote in front of
Gideon */ ");


[quoted text, click to view]

Uri Dimant
5/6/2007 12:00:00 AM
giddy,shalom
Looks like cmd.ExecuteNonQuery(); is not executing. Can you open Profiler
to see what is happening while you perform an INSERT?



[quoted text, click to view]

giddy
5/6/2007 4:15:54 AM
Hi Mike, Shalom! Uri

I figured it out.I'm sorry! im pretty embarrased! lol it was pretty
simple!

Acutally the quotes were fine. I just made typos while putting it
here. Yea , ExecuteNonQuery() returns 1.

I only kept checking the database from the ServerExplorer , which was
the Database in the core project folder. Vs made a *copy of the
database in this test project folder* and it inserted data in there.
(My connection string searches fr the DB in the running .exe dir and
uses AttachDbFilename)

It works fine now. Linked to the correct database.

Thanks so much

Gideon
AddThis Social Bookmark Button