84944035977K00W||1551|03||1|0||||||20180425175335|20180425|1|0|0|8001155120180425175345200996|| 84937045462K000||1537|30||1000|4||||134596132607191260||20180425134459|20180425|1|2|0|8001153720180425134458089073|| 84937045462K000||1537|30||200|1||||134562791343004861||20180425185108|20180425|1|0|0|8001153720180425185103226837|| 84937045462K000||1537|30||2100|8||||135044265261257765||20180425134120|20180425|1|4|0|8001153720180425134118087429|| 84944135311K01N||1537|30||700|3||||134651152119857243||20180425130147|20180425|1|1|0|8001153720180425130145069364|| 84944015812K02H||1537|30||25000|95||||134523506884150876||20180425135012|20180425|1|51|0|8001153720180425135010091525|| 84944015812K02H||1537|30||61200|233||||134585242946985751||20180425120359|20180425|1|125|0|8001153720180425120351044674|| 84944135311K01N||1537|30||250|1||||135532872772681822||20180425090137|20180425|1|1|0|8001153720180425090135861341|| 84937045462K000||1537|30||650|2||||135066416081633428||20180425121605|20180425|1|1|0|8001153720180425121603049893|| 84944135311K01N||1537|30||1160|4||||134981137645301948||20180425120617|20180425|1|2|0|8001153720180425120616045662|| 根据这个格式,根据分隔符,写入数据库,用C#
时间: 2023-08-16 14:12:01 浏览: 82
根据给定的格式和分隔符将数据写入数据库的示例代码如下:
```csharp
using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
string data = "84944035977K00W||1551|03||1|0||||||20180425175335|20180425|1|0|0|8001155120180425175345200996||" +
"84937045462K000||1537|30||1000|4||||134596132607191260||20180425134459|20180425|1|2|0|8001153720180425134458089073||" +
"84937045462K000||1537|30||200|1||||134562791343004861||20180425185108|20180425|1|0|0|8001153720180425185103226837||" +
"84937045462K000||1537|30||2100|8||||135044265261257765||20180425134120|20180425|1|4|0|8001153720180425134118087429||" +
"84944135311K01N||1537|30||700|3||||134651152119857243||20180425130147|20180425|1|1|0|8001153720180425130145069364||" +
"84944015812K02H||1537|30||25000|95||||134523506884150876||20180425135012|20180425|1|51|0|8001153720180425135010091525||" +
"84944015812K02H||1537|30||61200|233||||134585242946985751||20180425120359|20180425|1|125|0|8001153720180425120351044674||" +
"84944135311K01N||1537|30||250|1||||135532872772681822||20180425090137|20180425|1|1|0|8001153720180425090135861341||" +
"84937045462K000||1537|30||650|2||||135066416081633428||20180425121605|20180425|1|1|0|8001153720180425121603049893||" +
"84944135311K01N||1537|30||1160|4||||134981137645301948||20180425120617|20180425|1|2|0|8001153720180425120616045662||";
char delimiter = '|';
string connectionString = "YourConnectionString"; // 数据库连接字符串
UploadDataToDatabase(data, delimiter, connectionString);
Console.WriteLine("数据上传完成!");
}
static void UploadDataToDatabase(string data, char delimiter, string connectionString)
{
string[] rows = data.Split(new[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
foreach (string row in rows)
{
string[] values = row.Split(delimiter);
// 根据实际情况修改SQL语句和参数化查询
SqlCommand command = new SqlCommand("INSERT INTO YourTable (Column1, Column2, Column3, Column4, Column5) VALUES (@Value1, @Value2, @Value3, @Value4, @Value5)", connection);
command.Parameters.AddWithValue("@Value1", values[0]);
command.Parameters.AddWithValue("@Value2", values[1]);
command.Parameters.AddWithValue("@Value3", values[2]);
command.Parameters.AddWithValue("@Value4", values[3]);
command.Parameters.AddWithValue("@Value5", values[4]);
// 添加更多参数...
command.ExecuteNonQuery();
}
}
}
}
```
请将代码中的 `YourConnectionString` 替换为你的数据库连接字符串,`YourTable` 替换为你要插入数据的表名,并根据实际情况修改SQL语句和参数化查询。
这只是一个简单的示例代码,假设每行数据有5个字段。你可以根据实际情况修改代码以适应你的数据和表结构。希望对你有所帮助!
阅读全文