Jump to content
  • Advertisement
Sign in to follow this  

[.net] C# SQL Insert Command.. Not working?

This topic is 3658 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm quite new and have been looking for good advice! Lucky you. Anywayss, I'm writing a program which has to INSERT data into my Access database. Simple, atleast it should be. Having trouble though, its not actually doing it. Can't figure it out either, I assume it mys SQL statement. I don't know how else I'd write it at least. Permissions may be an issue, I'm not sure. Heres the code:
            Int64 id, ctele;
            id = Convert.ToInt64(lbl_add_Id.Text);
            string cname, caddress, cpost;
            cname = txt_Add_Name.Text;
            caddress = txt_Add_Address.Text;
            cpost = txt_Add_PC.Text;
            ctele = Convert.ToInt64(txt_Add_Tele.Text);

   //SQL adapter to gain access to the database, including the SQL statement
            OleDbDataAdapter da_Customer_View = new OleDbDataAdapter(
                  "INSERT INTO tbl_Customers" +
                  "VALUES('" + (id) + "'" + ",'" + (cname) + "','" +                 (caddress) + "','" + (cpost) + "','" + (ctele) + "')",
                  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents and Settings\\Me\\My Documents\\"
                  + "SQL Databases\\XMAS SQL Database\\FoodTastic.accdb");

Any idas?

Share this post

Link to post
Share on other sites
First problem I see is:

"INSERT INTO tbl_Customers" +

"VALUES('" + (id) + "'" ...

Without the +, this works out to:

"INSERT INTO tbl_CustomersVALUES('id' ..."

I'd suggest you use string.Format() to build your strings like these, so you can just do:

string s = string.Format("INSERT INTO {0} VALUES ('{1}','{2}','{3}' ...")...

It'll make problems like this a lot easier to find.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!