none
how to get data from file to c# RRS feed

  • Question

  • hello ,

    i need to know how to get data from file not from sql server to c# application 

    Saturday, July 7, 2018 11:32 PM

All replies


  • i need to know how to get data from file not from sql server to c# application 

    What kind of file? The method(s) used may vary accordingly.

    Is it a "text" file? Or a "binary" file? Is it formatted or unformatted?
    Does the data in the file follow some specific standard? 
    e.g. - html, xml, etc.

    - Wayne

    Sunday, July 8, 2018 12:42 AM
  • file will contain important data i don't need the user to know it and the file winn contain and tables and columns like sql database but i don't need to connect to sql server i need it with the program file 
    Sunday, July 8, 2018 5:09 AM
  • I think if you have data layer as interface which can get data from database you can rewrite it to get data from file easily. 

    What purpose you have that you want not to use database? You can use SQLlite or something similar. I have image about "database" in text files but it could be night mare to work with data. 

    Sunday, July 8, 2018 9:26 AM
  • You Can use a Text File to Save Data. in This Example, We Have this Text File :

    ;1,John,Smith;2,David,Smith

    The ";" sign, Separate Rows and "," Separate Columns. Write This Code For Load Data :

    if (!File.Exists(@"C:\Persons.txt"))
    {
        File.Create(@"C:\Persons.txt");
    }
    
    StreamReader sr = new StreamReader(@"C:\Persons.txt");
    string file = sr.ReadToEnd();
    sr.Close();
    
    DataTable dt = new DataTable();
    dt.Columns.Add("ID");
    dt.Columns.Add("Name");
    dt.Columns.Add("LastName");
    
    string[] rows = file.Split(';');
    
    for (int i = 0; i < rows.Length; i++)
    {
        string[] cells = rows[i].Split(',');
        if (cells[0] != "")
            dt.Rows.Add(cells[0], cells[1], cells[2]);
    }
    
    dgv.DataSource = dt;

    To add a Row To Text File, you Can Use This Code :

    StreamReader sr = new StreamReader(@"C:\Persons.txt");
    string Data = sr.ReadToEnd();
    sr.Close();
    StreamWriter sw = new StreamWriter(@"C:\Persons.txt");
    sw.Write(Data + ";" + txt_ID.Text + "," + txt_Name.Text + "," + txt_LName.Text);
    sw.Close();


    Sunday, July 8, 2018 1:13 PM
  • >>the file winn contain and tables and columns like sql database <<

    It's pretty useless to try and guess what format your file is in (tables & columns can mean just about anything), so any replies with any suggestions is not going to help anyone, until we have more information:

    Not only do we need to know the format of the actual file (post some sample data from your file, please) ... but we also need to know how you plan to represent that data in your application.

    You have to answer these two questions before any useful suggestions can be made.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, July 8, 2018 4:37 PM
    Moderator

  • i get all this info(images and vchar "i can't send a photo") from sql database but if i send this program to another pc he cant connect to sql server so i need to put data in file with the program then he dont need to connect to sql server 

    Sunday, July 8, 2018 5:21 PM
  • The data contains images? So, there's binary data in the file, along with other kinds of data. And you've gotten the data from a sql database (which is why you probably said it is in tables & columns).

    Next question ... since you have originally gotten the data from a database, did you read it into a DataSet?

    If so, you can write it into an xml file quite easily:

    MyDataSet.WriteXml("MyDataSet.xml");

    Then use this file with the program on the other computer (the one that doesn't have SQL connectivitiy). Read the file into the program using this syntax:

    MyDataSet.ReadXml("MyDataSet.xml");

    Another way to handle this whole thing is to use a client/server architecture ... in other words, the program on the other computer calls a web service that can deliver the data to it. The web service is the program that will take care of getting the data from the database.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, July 8, 2018 7:45 PM
    Moderator
  • Hi,

    Maybe you could consider NoSQL db, such as MongoDB, which is very popular. A NoSQL database provides a mechanism for storage and retrieval of data that is modeled in means other than the tabular relations used in relational databases.

    For more information, please refer to:

    https://docs.mongodb.com/ecosystem/drivers/csharp/

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 9, 2018 5:11 AM
    Moderator
  • Hi,

    If the other machine cannot connect to the source SQL storage, then no need to build your database on SQL server. Alternate options is using Access database or local DB (service based database).

    The service based database, is a local SQL database file (.mdf) which can be attached with your desktop application and you can run T-SQL statements on it as well.

    I found you a quick demo on youtube for the service based database:
    https://www.youtube.com/watch?v=R2GAgr4Zlk0

    Good luck!

    • Marked as answer by joshMohamed Monday, July 9, 2018 9:25 PM
    • Unmarked as answer by joshMohamed Monday, July 9, 2018 9:29 PM
    • Marked as answer by joshMohamed Tuesday, July 10, 2018 7:50 AM
    • Unmarked as answer by joshMohamed Tuesday, July 10, 2018 8:52 AM
    Monday, July 9, 2018 4:34 PM
  • thank you for reply 
    okay i saw the video this what i need , but there is another thing i need to know its when i creat .mdf file how i can connect to it 

    for example i connect to sqlserver string conn; , how can i connect to the file i build all my program on sql if i change it to .mdf file did i need to change all code or there is any way make me didn't change it 


    and if i send the program to another pc did it will run without needing to download sql program(SQLEXPRESS or MSSQLSERVER) on it ?
    • Edited by joshMohamed Monday, July 9, 2018 9:38 PM
    • Marked as answer by joshMohamed Tuesday, July 10, 2018 7:35 AM
    • Unmarked as answer by joshMohamed Tuesday, July 10, 2018 7:50 AM
    Monday, July 9, 2018 9:29 PM
  • Hi hazemMohamedAzzam,

    >>okay i saw the video this what i need , but there is another thing i need to know its when i creat .mdf file how i can connect to it 

    You could connect access with connection string like SQL Server, but you use OleDb instead of SqlClient

    Here is a sample for your reference.

    https://www.c-sharpcorner.com/UploadFile/puranindia/ado-net-application-using-ms-access-2007-database/

    >>and if i send the program to another pc did it will run without needing to download sql program(SQLEXPRESS or MSSQLSERVER) on it ?

    No, we just copy the access to another pc without running any sql. if we Microsoft.ACE.OLEDB.12.0, we need to install related provider  Microsoft Office Access database engine 2007 and Microsoft Access Database Engine 2010 Redistributable

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 10, 2018 1:07 AM
    Moderator
  • okay i think we should install sql on another pc cuz when i sent it and run it on another pc i get this error 

    network related or instance specific error occurred while establishing a connection to sql server

    Tuesday, July 10, 2018 8:52 AM
  • Hi joshMohamed,

    Why not consider office Access to storage your records.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 13, 2018 5:17 AM
    Moderator