Click here to Skip to main content
15,846,038 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

Inertion failed to while passing a large string to SQL query

String as follows:

%PDF-1.6
%����
69 0 obj
<</Linearized 1/L 777207/O 71/E 86298/N 13/T 776799/H [ 447 172]>>
endobj
75 0 obj
<</DecodeParms<</Columns 3/Predictor 12>>/Filter/FlateDecode/ID


its having around 7 lakhs charecters like this . Whether I can insert into SQL using insert query ?
Here is my code as follows:
string readText = File.ReadAllText(@"D:\olefy001.txt");
           SqlCommand cmd = new SqlCommand("insert into FileContent values(" + readText + ")", con);
           cmd.ExecuteNonQuery();


What I have tried:

Using Bulk insert its working ! But I have to pass it as string variable inside insert query is it possile ?

Select query is not fetching all elements after inserting using bulk insert what to do ?
Posted
Updated 21-Nov-17 7:32am
Comments
Richard MacCutchan 21-Nov-17 9:09am    
A PDF file should not be treated as a string, and it is not efficient to store such large records in your database. Better to save the file in your server and store the address of the file in the database.
ali_1 21-Nov-17 11:03am    
Thank you , its working , I found another way to store

SqlCommand cmd = new SqlCommand("insert into FileContent values(" + readText + ")", con);

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
v2
Comments
ali_1 21-Nov-17 11:03am    
Thank you for the information.I found another way to store this as string
Simple:
C#
string readText = File.ReadAllText(@"D:\olefy001.txt");
using (SqlCommand cmd = new SqlCommand("insert into FileContent values (@ReadText)", con))
{
    cmd.Parameters.AddWithValue("@ReadText", readText);
    cmd.ExecuteNonQuery();
}

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900