Skip to main content
gamedev.net
🔒 Locked

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

Started by David_harvey09 Dec 4, 2008 at 12:09 PM 0 replies 1.2k views
Original Post
David_harvey09
David_harvey09
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");
            lst_New_Customers.Items.Clear();
            UpdateCustomer();

Any idas?
Spodi
Spodi
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.
NetGore - Open source multiplayer RPG engine

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.