如何从一个文本文件中读取数据连接字符串和多个 SQL 命令,并将输出保存在 C# 中的输出.txt中

本文关键字:输出 txt 命令 SQL 存在 保存 连接 一个 文本 数据 读取 | 更新日期: 2023-09-27 18:33:20

我有一个输入.txt文件,它的第一行是数据库连接字符串(Access DB),并继续执行多个sql命令。

我必须创建一个程序,从此输入中读取数据库连接字符串.txt文件建立连接,然后从此输入中逐个读取sql命令.txt并单独执行,将所有事务的结果保存在输出.txt文件中。

我成功地建立了联系,但我不知道如何完成其余的工作。

我的代码是:

{
    private static int i;
    static void Main(string[] args)
    {
        // Making connection to Access database via input.txt
        string fileLoc = @"C:''input.txt";
       // reading input.txt
        String pro;
        using (StreamReader sr = new StreamReader(fileLoc))
        {
            String line = sr.ReadLine();
            pro = line;
        }
        OleDbConnection con = null;
        try
        {
            con = new OleDbConnection(pro);
            con.Open();
            // trying new one
            String command;
            using (StreamReader csr = new StreamReader(@"C:''commands.txt"))
            {
                String line = csr.ReadLine();
                pro = line;
            }
            OleDbConnection cCon = null;

            // Execute SQL Commands 
            OleDbCommand comm = con.CreateCommand();
            comm.CommandText = pro;
            //Read and process data rows
            IDataReader reader = comm.ExecuteReader();
            object[] dataRow = new object[reader.FieldCount];
            string[] write = {"Maria", " Hafeez"};
            //string m = "      | {0} ";
            while (reader.Read())
            {
                int cols = reader.GetValues(dataRow);
                for (int i = 0; i < cols; i++)
                {
                    Console.Write("      | {0} ", dataRow[i]);
                }
                Console.Write("'n");
                //write = dataRow[i];
            }
            Console.WriteLine();
            Console.ReadKey();
            string path =@"C:''output.txt";
            if (!File.Exists(path))
            {
                //output in output.txt
                File.WriteAllLines(path, write);
            }
            //Release resources and close connection   
            reader.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            Console.ReadKey();
        }
        finally
        {
            try
            {
                if (con != null)
                    // ----- close connection 
                    con.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }

}

如何从一个文本文件中读取数据连接字符串和多个 SQL 命令,并将输出保存在 C# 中的输出.txt中

首先,这是"错误的",因为它会在您可以遍历文件之前关闭文件。

        String command;
        using (StreamReader csr = new StreamReader(@"C:''commands.txt"))
        {
            String line = csr.ReadLine();
            pro = line;
        }

例如,您必须在此过程中保持文件打开,或者读取所有行并存储在列表中。

在第一种情况下(文件仍然打开),你可以这样做:你必须"迭代"文件的行。

// Making connection to Access database via input.txt
string fileLoc = @"C:''input.txt";
// reading input.txt
String pro;
using (StreamReader sr = new StreamReader(fileLoc))
{
    String line = sr.ReadLine();
    pro = line;
    OleDbConnection con = null;
    con = new OleDbConnection(pro);
    con.Open();
    string line;
    while((line = csr.ReadLine()) != null)
    {
        // Execute SQL Commands 
        OleDbCommand comm = con.CreateCommand();
        comm.Connection = con;
        comm.CommandText = line; //<<= this to get the actual line in the file
        //Read and process data rows
        IDataReader reader = comm.ExecuteReader();
        object[] dataRow = new object[reader.FieldCount];
        string[] write = {"Maria", " Hafeez"};
        //string m = "      | {0} ";
        while (reader.Read())
        {
            int cols = reader.GetValues(dataRow);
            for (int i = 0; i < cols; i++)
            {
                Console.Write("      | {0} ", dataRow[i]);
            }
            Console.Write("'n");
            //write = dataRow[i];
        }
        Console.WriteLine();
        Console.ReadKey();
        string path =@"C:''output.txt";
        if (!File.Exists(path))
        {
            //output in output.txt
            File.WriteAllLines(path, write);
        }
        //Release resources and close connection   
        reader.Close();
    }
}

我认为这就足够了,不是吗?其余的你可以弄清楚(尝试捕获块,关闭连接等)