How to read text file in c# and store in database console ap
i want to read text from text file and save text file data into my SQL database how can i do this any idea? i am new to the c# languange
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Biswa Pujarini MohapatraRank #465 · 5.4K reputationPosted Jan 31, 2014, 4:50 PM EST
hope it helps you, let me know if you need anything else
Biswa Pujarini MohapatraRank #465 · 5.4K reputationPosted Jan 28, 2014, 9:40 AM EST
class Test
{
public static void Main()
{
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
this is how you can read your data from textfile. Can you paste your data format in textfile how it is, so that i can give you the exact code
to insert into Db
Ali Ashiq61 reputationPosted Jan 29, 2014, 2:03 AM EST
Here is my text file information, and i want to insert these information in my SQL Database, Having fileds Product_Name, Product_Price, RstName
and these information seprated by Comma Separated Value, and First color insert in Product_Name, and 280 insert in Product_Price and McDonald in RstName.
Ali Ashiq61 reputationPosted Jan 31, 2014, 8:34 AM EST
this error is showing!
VulpesRank #55 · 29.2K reputationPosted Jan 31, 2014, 12:30 PM EST
SqlCommand cmd = new SqlCommand("INSERT INTO Products(Product_Name, Product_Price, RstName) VALUES (@Product_Name, @Product_Price, @RstName)", con);
Ali Ashiq61 reputationPosted Jan 31, 2014, 10:05 PM EST
let suppose 4/1000=250 and now 250 is , match in Product_Price Field in RstProductDetails, and select only Restaurant where 250 is matched. Next when a user select Restaurant e.g KFC and then KFC Products details is show.
Here is my [WebMethod]
Ali Ashiq61 reputationPosted Jan 31, 2014, 10:09 PM EST
Ali Ashiq61 reputationPosted Jan 31, 2014, 11:16 PM EST
Ali Ashiq61 reputationPosted Feb 1, 2014, 7:00 AM EST
Ali Ashiq61 reputationPosted Feb 1, 2014, 7:30 AM EST
Biswa Pujarini MohapatraRank #465 · 5.4K reputationPosted Feb 1, 2014, 5:10 PM EST
I am not so clear about your requirement.
As per my understanding you are looking for two methods one will return matched resturants based on product price entered by user and then user can select one resturant and it will display product details. is it so, please confirm
Ali Ashiq61 reputationPosted Feb 2, 2014, 1:57 AM EST
now the scrapped text file is have only three fields i have attached text file as well.
I have a question how could we add RstID automatically please give me idea.
Ali Ashiq61 reputationPosted Feb 2, 2014, 2:08 AM EST
Biswa Pujarini MohapatraRank #465 · 5.4K reputationPosted Feb 2, 2014, 12:04 PM EST
Ali Ashiq61 reputationPosted Feb 3, 2014, 9:43 AM EST
SELECT * FROM RstProductDetails WHERE Product_Price <='300'
when i execute this query it will show all records which is less than or greater than 300. I am shocked about this, and Product_Price (nvarchar), because when we scrapped websites through scrapper code it will store automatically in the database ans the price format on different websites is e.g "Rs 600" or "Rs 1,300".
i have a price in both format "2000" or "2,000" so in which datatype i select for Product_price that will give me exact Product which is <=300
Ali Ashiq61 reputationPosted Feb 3, 2014, 10:10 AM EST
Ali Ashiq61 reputationPosted Feb 4, 2014, 4:15 AM EST
Biswa Pujarini MohapatraRank #465 · 5.4K reputationPosted Feb 4, 2014, 9:34 PM EST
here is your sql command
1) to get restaurant name
Select Rstname from ResturantNames R(nolock) join Resturantproducts rp
on r.rst_id = rp.rst_id where rp.price <='300'
2) to get product details based on Resturant name
Select rp.* from Resturantproducts Rp(nolock) join Resturantnames r
on r.rst_id = rp.rst_id where r.name <='Mcdonald'
Ali Ashiq61 reputationPosted Feb 5, 2014, 1:42 AM EST
Biswa Pujarini MohapatraRank #465 · 5.4K reputationPosted Feb 5, 2014, 2:58 AM EST
Ali Ashiq61 reputationPosted Feb 5, 2014, 9:19 AM EST
Biswa Pujarini MohapatraRank #465 · 5.4K reputationPosted Feb 5, 2014, 10:05 AM EST
Ali Ashiq61 reputationPosted Feb 5, 2014, 10:29 PM EST
Here is my new methods to get two methods one will return matched resturants based on product price entered by user and then user can select one resturant and it will display product details. When i debug my web services it will show nothing except empty xml
Biswa Pujarini MohapatraRank #465 · 5.4K reputationPosted Feb 5, 2014, 11:36 PM EST
Ali Ashiq61 reputationPosted Feb 6, 2014, 12:14 AM EST
Biswa Pujarini MohapatraRank #465 · 5.4K reputationPosted Feb 6, 2014, 12:22 AM EST
let me know if you found any issue
Ali Ashiq61 reputationPosted Feb 6, 2014, 1:24 AM EST
Invalid column name 'RstID' this error occur while debugging web services, but i fount this error according to your suggested in above comments, i put break point and debug the code i will show me the line error, its really help me, but now it will show the list of all restaurant with 129 rows. According to the method it will show only one restaurant name which is in HotelNames table, now whats our next stepBiswa Pujarini MohapatraRank #465 · 5.4K reputationPosted Feb 6, 2014, 2:00 AM EST
Ali Ashiq61 reputationPosted Feb 7, 2014, 9:19 PM EST
Ali Ashiq61 reputationPosted Feb 7, 2014, 10:46 PM EST
Ankita Patel2 reputationPosted Mar 4, 2016, 10:50 AM EST
Suresh Reddy28 reputationPosted Jan 10, 2018, 2:46 AM EST
Nitin SontakkeRank #264 · 9.6K reputationPosted Jan 10, 2018, 2:51 AM EST
Biswa Pujarini MohapatraRank #465 · 5.4K reputationPosted Jan 30, 2014, 10:13 PM EST
Please change your connection string and table accordingly