简体   繁体   中英

How to save items of list with their index in text file using c#

I have created a list in c#, now I need to save the list in text file with the index for each item in the list? please explain with a simple example.

Try this Code: i hope you will get the basic idea from it.

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> _names = new List<string>()
            {
                "Rehan",
                "Hamza",
                "Adil",
                "Arif",
                "Hamid",
                "Hadeed"
            };

            using (StreamWriter outputFile = new StreamWriter(@"E:\test.txt")
            {
                foreach (string line in _names)
                    outputFile.WriteLine(line);
            }
        }
    }
}

OR you should try for loop as well.

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> _names = new List<string>()
            {
                "Rehan",
                "Hamza",
                "Adil",
                "Arif",
                "Hamid",
                "Hadeed"
            };

            using (StreamWriter outputFile = new StreamWriter(@"E:\test.txt")
            {
                for (int index = 0; index < _names.Count; index++)
                    outputFile.WriteLine("Index : " + index + " - " + _names[index]);
            }
        }
    }
}

According to your Comment Below: How to save a list data into a SQL Server table. You can follow the same principle from the above code:

Code:

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Table 
            // -------------
            // | ID | Name |
            // -------------

           // Please Not that: ID Column in a database should not be identity Colomn because in this example i am going to add data to ID Column explicity...                

            // List of name that we are going to save in Database.
            List<string> _names = new List<string>()
            {
                "Rehan",
                "Hamza",
                "Adil",
                "Arif",
                "Hamid",
                "Hadeed"
            };

            SqlConnection connection = new SqlConnection("Connection string goes here...");
            connection.Open();
            for (int index = 0; index < _names.Count; index++)
            {
                SqlCommand command = new SqlCommand("INSERT INTO tbl_names (id,name) VALUES ('"+index+"', '"+_names[index]+"')",connection);
                command.ExecuteNonQuery();
            }
            connection.Close();
        }
    }
}

Note: by using this syntax new SqlCommand("INSERT INTO tbl_names... there is a Chance of SQL Injection so by avoiding that you can use Store Procedure instead....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM