Solved

C# Insert String SQL

Posted on 2004-08-15
2
1,066 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
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.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

685 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