Solved

Get rid of the ' in a query

Posted on 2006-11-22
3
197 Views
Last Modified: 2012-08-13
Hi im having a problem with importing data from access into SQL;

It does not like Mc'Smith or in other words '

using the following:

SqlCeConnection connection = new SqlCeConnection(ConnectionString);
                    connection.Open();

                    foreach (DataRow row in ds.Tables[0].Rows)
                    {

                        SqlCeCommand command = connection.CreateCommand();

                        //SqlCeCommand sqlCom = new SqlCeCommand();
                        string strtitle = row["Title"].ToString();
                        string strinitials = row["Initials"].ToString();
                        string strlastname = row["Surname"].ToString();
                        string straddress = row["Address 1"].ToString();
                        string straddress1 = row["Address 2"].ToString();
                        string strtown = row["Address 3"].ToString();
                        //string strcounty = row["county"].ToString();
                        string strpostcode = row["postcode"].ToString();
                        string strcountry = row["country"].ToString();
                        string stremail = row["Email"].ToString();
                        string strtel = row["phone number"].ToString();
                        //string strhdyhau = row["hdyhau"].ToString();
                        //string stripadd = row["ipadd"].ToString();
                        string strcate = "Explorers";
                        //string strdaterecieved = row["daterecieved"].ToString();
                        string strsubscribe = "No";
                        string strdelmethod = "By Post";
                       
                        string strimportfrom = "Source";
                        string strclientenabled = "Yes";
                        string strbrochureSubscibe = "Yes";



                        SqlCeParameter param = new SqlCeParameter();
                        param.ParameterName = "@MyParam";
                        param.Value = (DateTime)row["Reservation Date"];
                        command.Parameters.Add(param);

                        command.CommandText = @"INSERT INTO Eaddress (title, name, lastname, address, address1, town, postcode, country, emailad, tel, subscribeemail, cate, importeddate, EnquireDate, importedfrom, delmethod, clientEnabled, subscribebrochure) VALUES ('" + strtitle + "', '" + strinitials + "', '" + strlastname + "', '" + straddress + "', '" + straddress1 + "', '" + strtown + "', '" + strpostcode + "', '" + strcountry + "', '" + stremail + "', '" + strtel + "', '" + strsubscribe + "', '" + strcate + "', GETDATE(), @MyParam, '" + strimportfrom + "', '" + strdelmethod + "', '" + strclientenabled + "', '" + strbrochureSubscibe + "')";
                        command.ExecuteNonQuery();



                    }

can someone show me how to either ignore the ' , take it out or insert it (its the Surname Field i have the problem)

thanks
0
Comment
Question by:ziwez0
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 17998819
for all of the fields that are string data, do like this:
 string strtitle = row["Title"].ToString().Replace("'", "''");

now, you seem to be copying the rows from 1 data table to another database, are these 2 different databases? doing a INSERT INTO ... SELECT ... FROM  should be faster ?!
if that is not possible, you still might look up a parametrized query, that will take the burden from the quotes from you.


0
 
LVL 2

Author Comment

by:ziwez0
ID: 17998910

yeah would be got some other stuff going on to, im getting in length error how do i say keep the first 10 digits then trim everything after

something like..

string strLastname = row["Surname"].ToString().TrimEnd(10);
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17998994
trimend is to trim away characters at the end of the string, and the parameter is to indicate which character...

you want to use this:
string strLastname = row["Surname"].ToString().Substring(0,10);
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now