如何从控制台应用程序读取/写入SQL数据库(C#)

时间:2017-08-30 07:55:06

标签: c# sql .net sql-server

我是SQL服务器的新手。

正如您所看到的,我上面的第一个函数仅用于调用第二个函数并将列表作为参数传递 现在我正在解析来自divideTrans char数组的信息。 每个值都意味着什么,我正在向控制台显示。 但是,我需要将其保存到SQL数据库。解析的值。 我在SQLite方面有一些经验,但这有点不同。 我搜索了一些东西,这是我能想到的。但是,我发现没有简单的方法来更新列。我的列名与console.writeline代码中提到的相同,例如,一些列是“data record”,“record_id”,“article_code”等。所以我希望保存到列“数据记录”例如,“2.1运动记录”的值,然后在“record_id”中,divideTrans [0]的值。有人可以帮我一个有效的方法吗?

class save_parsed_data
{
    static string connectionstring;
    static SqlConnection connection; 

    public static void save_data(string transaction)
    {
        connectionstring = ConfigurationManager.
               ConnectionStrings["testEnvironmentSAMPLE.Properties.Settings.sampleConnectionString"].ConnectionString;

            List<char> dividedTrans = new List<char>(transaction.ToCharArray());

            if (transaction.StartsWith("21"))
                StringWith21(dividedTrans);

            Console.ReadKey();
    }

    public static void StringWith21(List<char> dividedTrans)
    {
        using (connection = new SqlConnection(connectionstring))
        using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Sample_values", connection))
        {   
            DataTable codaTable = new DataTable(); 
            adapter.Fill(codaTable);

            Console.WriteLine("\nData Record 2.1: Movement Record.");

            Console.WriteLine("Record Identification: {0}", dividedTrans[0]);

            Console.WriteLine("Article Code: {0}", dividedTrans[1]);

            Console.WriteLine("Sequence Number: {0}", new string(dividedTrans.GetRange(2, 4).ToArray()));

            Console.WriteLine("Detail Number: {0}", new string(dividedTrans.GetRange(6, 4).ToArray()));

            Console.WriteLine("Reference Number of bank: {0}", new string(dividedTrans.GetRange(10, 21).ToArray()));

            Console.WriteLine("Movement Sign: {0}", new string(dividedTrans.GetRange(31, 1).ToArray()));

            Console.WriteLine("Amount: {0}", Convert.ToString(Int32.Parse(string.Join("", dividedTrans.GetRange(32, 15)))));

            Console.WriteLine("Value Date: {0}", DateandTime(string.Join("", dividedTrans.GetRange(47, 6))).ToString("d"));

            Console.WriteLine("Transaction Code: {0}", new string(dividedTrans.GetRange(53, 8).ToArray()));

            Console.WriteLine("Communication Type: {0}", new string(dividedTrans.GetRange(61, 1).ToArray()));

            Console.WriteLine("Communication zone: {0}", new string(dividedTrans.GetRange(62, 53).ToArray()));

            Console.WriteLine("Entry Date: {0}", DateandTime(string.Join("", dividedTrans.GetRange(115, 6))).ToString("d"));

            Console.WriteLine("Sequence Number: {0}", new string(dividedTrans.GetRange(121, 3).ToArray()));

            Console.WriteLine("Globalization Code: {0}", new string(dividedTrans.GetRange(124, 1).ToArray()));

            Console.WriteLine("Next Code: {0}", new string(dividedTrans.GetRange(125, 1).ToArray()));

            Console.WriteLine("Blank: {0}", new string(dividedTrans.GetRange(126, 1).ToArray()));

            Console.WriteLine("Linker Code: {0}", new string(dividedTrans.GetRange(127, 1).ToArray()));
        }
    }

}

2 个答案:

答案 0 :(得分:1)

我的建议是使用一些ORM(对象关系映射)工具。对于C#,有2个主要的EntityFramework和NHibernate。为了简单起见,我首选EntityFramework。

答案 1 :(得分:1)

有很多方法可以写入数据。

一种方法是原始SQL,如SQL update statement in C#

所示
{{1}}