C# Insert String SQL

Posted on 2004-08-15
Last Modified: 2010-05-18
I have a slight problem inserting the proper result into an MS-SQL table. The result should be <14> however all I get is PRE. BTW, the SQL command is not broken appart like you see it on this limited width posting..........

I thought using single quotes desiganted a 'string'? Help........

string pattern1 = @"(<\d{2,3}>)";

Regex r1 = new Regex(pattern1, RegexOptions.Compiled);

Match PRE = r1.Match(stringOfData);
SqlDataAdapter1.SelectCommand.CommandText = "INSERT INTO
tblMyUdpServer (col_PRE) VALUES ('PRE')";
catch(SqlException error)
Question by:kvnsdr

Accepted Solution

shovavnik earned 125 total points
ID: 11804328
try this sql statement instead:

"INSERT INTO tblMyUdpServer (col_PRE) VALUES ('" + PRE + "')";

You're concatenating strings to produce a sql statement.

If that doesn't work, try this:

"INSERT INTO tblMyUdpServer (col_PRE) VALUES ('" + PRE.ToString() + "')";


Expert Comment

ID: 11807188
just an fyi, please try to use the parameter object to pass values into an insert statement.  A parameter will take care of special characters for you.  Otherwise you are setting yourself up for a possible SQL injection attack.

see the bottom of this article for some info on using parameters:;en-us;301075

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
delphi parse string to params 3 122
What is Python programming? 3 113
Counting documents in a Domino View 3 83
Hide vba in gp 7 94
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

770 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