txt to server
主要是sql语句和添加参数和文本分隔符的部分
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace readtxttoxml
{
internal class Program
{
static void Main(string[] args)
{
string connectionString = "Data Source=localhost;Initial Catalog=scnt;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
string[] lines = File.ReadAllLines(@"D:/DDD/students.txt");
foreach (string line in lines)
{
string[] fields = line.Split('\t');
string query = "INSERT INTO student (id, name, gender) VALUES (@Value1, @Value2, @Value3)";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.Add("@Value1", SqlDbType.Int);
command.Parameters["@Value1"].Value = int.Parse(fields[0]);
command.Parameters.Add("@Value2", SqlDbType.VarChar);
command.Parameters["@Value2"].Value = fields[1];
command.Parameters.Add("@Value3", SqlDbType.VarChar);
command.Parameters["@Value3"].Value = fields[2];
int rowsAffected = command.ExecuteNonQuery();
Console.WriteLine($"修改了{rowsAffected}行");
}
connection.Close();
}
}
}