SIGN UP MEMBER LOGIN:    
Resource

Frame SQL query using String formatters

Hemant Kumar Resources Dec 21, 2011
The main idea behind this resource is to frame our SQL Query using String Format Function. I think this is one of the best way to frame the SQL Query that i found. *** This code introduces SQL injection attacks and should not be used. ***

The string.Format method is a static method that receives a string that specifies where the following arguments should be inserted, and these are called substitutions.
In source code we need to use to frame the SQL Query
public bool SaveData(string firstName, string lastName)
    {
         
String connectionString = ConfigurationManager.ConnectionStrings["TESTDB"].ConnectionString;
        
bool result = false;
        
using (SqlConnection connection = new SqlConnection(connectionString))
        {
          
SqlCommand cmd = new SqlCommand();
            cmd.
Connection = connection;
             cmd.
CommandText = String.Format("insert into Test_DB.dbo.PersonName(FirstName,LastName) values ('{0}','{1}')", firstName, lastName);
             cmd.
CommandType = CommandType.Text;
            connection.
Open();
            
int count = cmd.ExecuteNonQuery();
            
if (count > 0)
            {
                result =
true;
            }
            
else
            {
                result =
false;
            }
        }
         
return result;
    }

Like this we can frame the Update and Delete Statements also.  Please refer this link formore about the String Format function.
share this resource :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Sponsored by
Nevron Gauge for SharePoint
Become a Sponsor