我是SQL服務器的新手。如何從控制檯應用程序(C#)讀取/寫入SQL數據庫
正如你所看到的,我上面的第一個函數只是用來調用第二個函數並傳遞一個列表作爲參數。
現在我正在解析來自divideTrans字符數組的信息。 每個值都意味着某些東西,並且這是我顯示給控制檯的。 但是,我需要將其保存到SQL數據庫。解析的值。 我有一些SQLite的經驗,但是這有點不同。 我搜索了一些東西,這是我能想出來的。但是,我發現沒有簡單的方法來更新列。我的列名與console.writeline代碼中提到的名稱相同,例如某些列是「數據記錄」,「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()));
}
}
}
使用'實體framework'。 –
「保存到數據庫」意味着您需要插入到sql表中或進行更新。當你第一次將數據表讀入DataTable時,可能會得出結論,你將會更新它。或插入? – Serg
我需要將它插入表中。我稍後會更新它,但現在不行。現在桌子是空的。 –