Solved

C# Insert String SQL

Posted on 2004-08-15
2
1,062 Views
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);
{
try
{
connection1.Open();
SqlDataAdapter1.SelectCommand.CommandText = "INSERT INTO
tblMyUdpServer (col_PRE) VALUES ('PRE')";
SqlDataAdapter1.SelectCommand.ExecuteNonQuery();
connection1.Close();
}
catch(SqlException error)
{
MessageBox.Show(error.Message.ToString());
}
listBox1.Items.Add(PRE.ToString());
}
}
0
Comment
Question by:kvnsdr
2 Comments
 
LVL 8

Accepted Solution

by:
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() + "')";

0
 
LVL 5

Expert Comment

by:LlamaJoe
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:
http://support.microsoft.com/default.aspx?scid=kb;en-us;301075
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

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